Browse Source

会议图片上传

huwhois 3 years ago
parent
commit
a6ccaf2ad1

+ 27 - 4
src/main/java/io/renren/modules/admin/controller/MeetingImagesController.java

@ -1,5 +1,6 @@
1 1
package io.renren.modules.admin.controller;
2 2
3
import java.io.File;
3 4
import java.util.Arrays;
4 5
import java.util.Map;
5 6
@ -10,6 +11,7 @@ import org.springframework.web.bind.annotation.RequestBody;
10 11
import org.springframework.web.bind.annotation.RequestMapping;
11 12
import org.springframework.web.bind.annotation.RequestParam;
12 13
import org.springframework.web.bind.annotation.RestController;
14
import org.springframework.beans.factory.annotation.Value;
13 15
14 16
import io.renren.modules.admin.entity.MeetingImagesEntity;
15 17
import io.renren.modules.admin.service.MeetingImagesService;
@ -28,6 +30,8 @@ import io.renren.common.utils.R;
28 30
@RestController
29 31
@RequestMapping("admin/meetingimages")
30 32
public class MeetingImagesController {
33
    @Value("${fyhs-meeting.file.datapath}")
34
    private String dataPath; // 读取配置文件中的指定目录
31 35
    @Autowired
32 36
    private MeetingImagesService meetingImagesService;
33 37
@ -42,7 +46,6 @@ public class MeetingImagesController {
42 46
        return R.ok().put("page", page);
43 47
    }
44 48
45
46 49
    /**
47 50
     * 信息
48 51
     */
@ -79,10 +82,30 @@ public class MeetingImagesController {
79 82
    /**
80 83
     * 删除
81 84
     */
82
    @RequestMapping("/delete")
85
    @RequestMapping("/delete/{id}")
83 86
    @RequiresPermissions("admin:meetingimages:delete")
84
    public R delete(@RequestBody Integer[] ids){
85
		meetingImagesService.removeByIds(Arrays.asList(ids));
87
    public R delete(@PathVariable("id") Long id){
88
        MeetingImagesEntity meetingImages = meetingImagesService.getById(id);
89
90
        String filePath = dataPath + meetingImages.getUrl();
91
        String thumbPath =  dataPath + meetingImages.getThumb();
92
93
        File file = new File(filePath);
94
        File thumb = new File(thumbPath);
95
        if (file.exists()) {
96
            file.delete();
97
            System.out.println(meetingImages.getTitle() + " ===========删除成功=================");
98
        } else {
99
            System.out.println(meetingImages.getTitle() + "===============删除失败==============");
100
        }
101
        if (thumb.exists()) {
102
            thumb.delete();
103
            System.out.println(meetingImages.getTitle() + " ===========删除成功=================");
104
        } else {
105
            System.out.println(meetingImages.getTitle() + "===============删除失败==============");
106
        }
107
108
		meetingImagesService.removeById(id);
86 109
87 110
        return R.ok();
88 111
    }

+ 1 - 3
src/main/java/io/renren/modules/admin/service/impl/MeetingImagesServiceImpl.java

@ -3,8 +3,6 @@ package io.renren.modules.admin.service.impl;
3 3
import org.apache.commons.lang.StringUtils;
4 4
import org.springframework.stereotype.Service;
5 5
6
import java.util.HashMap;
7
import java.util.List;
8 6
import java.util.Map;
9 7
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
10 8
import com.baomidou.mybatisplus.core.metadata.IPage;
@ -27,7 +25,7 @@ public class MeetingImagesServiceImpl extends ServiceImpl<MeetingImagesDao, Meet
27 25
28 26
        IPage<MeetingImagesEntity> page = this.page(new Query<MeetingImagesEntity>().getPage(params),
29 27
                new QueryWrapper<MeetingImagesEntity>().eq("meeting_id", meetingId).like(StringUtils.isNotBlank(key),
30
                        "title", key));
28
                        "title", key).orderByDesc("sort").orderByAsc("id"));
31 29
32 30
        return new PageUtils(page);
33 31
    }

+ 74 - 34
src/main/java/io/renren/modules/app/controller/AppFileManagerController.java

@ -40,46 +40,85 @@ public class AppFileManagerController {
40 40
    private String uploadFolder; // 读取配置文件中的指定目录
41 41
42 42
    @PostMapping("/uploadimg")
43
    public R uploadImage(@RequestParam(value = "upload_file") MultipartFile file, HttpServletRequest request)
44
            throws IOException {
45
        boolean isThumb = false;
46
        String thumb = request.getParameter("isthumb");
47
        if (thumb != null && thumb != "" && thumb.equals("1")) {
48
            isThumb = true;
49
        }
50
        // 根据相对路径转化为真实路径
51
        // String rootpath =
52
        // request.getSession().getServletContext().getRealPath(File.separator);//
53
        // 获得web应用的绝对路径
54
        // File createFile = new File(rootpath + "../data/upload/");
55
        String activePath = request.getParameter("activepath");
56
        // 上传的图片只允许是 png、jpg 或 gif 中的格式
57
        if (file.getOriginalFilename().contains(".png") || file.getOriginalFilename().contains(".jpg")
58
            || file.getOriginalFilename().contains(".gif")) {
59
            Map<String, Object> resultSave = saveFile(file, activePath, false);
60
            
61
            if ((Integer) resultSave.get("status") != 0) {
62
                return R.error((String) resultSave.get("msg"));
43
    public R uploadImage(@RequestParam("upload_file") MultipartFile file, @RequestParam Map<String, Object> params) throws IOException {
44
        if (!file.isEmpty()) {
45
            boolean isThumb = false;
46
            boolean oldName = false;
47
            Integer width = 200;
48
            String thumb = (String) params.get("isthumb");
49
            if (thumb != null && thumb != "" &&  Integer.parseInt(thumb) == 1) {
50
                isThumb = true;
51
                if (params.containsKey("width")) {
52
                    width = Integer.parseInt((String) params.get("width"));
53
                }
54
            }
55
            String useOldName = (String) params.get("useOldName");
56
            if (useOldName != null && useOldName != "" &&  Integer.parseInt(useOldName) == 1) {
57
                oldName = true;
63 58
            }
64 59
65
            String filename = (String) resultSave.get("filename");
66
67
            Map<String, Object> result = new HashMap<String, Object>();
60
            // 根据相对路径转化为真实路径
61
            // String rootpath =
62
            // request.getSession().getServletContext().getRealPath(File.separator);//
63
            // 获得web应用的绝对路径
64
            // File createFile = new File(rootpath + "../data/upload/");
65
            String activePath = params.containsKey("activePath") ? (String) params.get("activepath") : null;
66
            String useUploadPath = "";
67
            if (activePath != null && !activePath.equals("")) {
68
                useUploadPath = activePath + "/";
69
            } else {
70
                // 使用配置文件中的指定上传目录与当前年月新建目录
71
                LocalDateTime ldt = LocalDateTime.now();
72
                String today = ldt.format(DateTimeFormatter.BASIC_ISO_DATE);
73
                useUploadPath = uploadFolder + "/" + today + "/";
74
            }
75
            String absolutePath = dataPath + useUploadPath; // 目录绝对路径
68 76
69
            result.put("picname", filename);
70
            if (isThumb) {
71
                int dot = filename.lastIndexOf(".");
72
                if ((dot >-1) && (dot < (filename.length()))) {
73
                    String suffix = filename.substring(dot + 1);
74
                    String thumbFilaname = filename.substring(0, dot)+ "_200." + suffix; 
75
                    Thumbnails.of(dataPath + filename).width(200).toFile(dataPath + filename);
76
                    result.put("thumb", thumbFilaname);
77
            // 上传的图片只允许是 png、 jpg、 webp 或 gif中的格式
78
            String fileOldName = file.getOriginalFilename();
79
            String suffix = fileOldName.substring(fileOldName.lastIndexOf(".") + 1);
80
            suffix = suffix.toLowerCase();
81
            if (suffix.equals("png") || suffix.equals("jpg") || suffix.equals("webp") || suffix.equals("gif")) {
82
                try {            
83
                    // 判断目录是否存在, 如果不存在则自动创建目录
84
                    File createFile = new File(absolutePath);
85
                    if (!createFile.exists()) {
86
                        createFile.mkdir();
87
                    }
88
    
89
                    String newFilename = "";
90
                    if (oldName) {
91
                        newFilename = fileOldName;
92
                    } else {
93
                        String uuid = UUID.randomUUID().toString().replace("-", "");// 随机生成一个唯一性的id 文件不重名
94
                        newFilename = uuid + "." + suffix;
95
                    }
96
    
97
                    File f = new File(absolutePath + newFilename);
98
                    if (f.exists()) {
99
                        return R.error("上传的文件已经存在,则提示用户重新上传图片或者重命名");  // 上传的文件已经存在,则提示用户重新上传图片或者重命名
100
                    } else {
101
                        file.transferTo(f); // 将上传的文件写入到系统中
102
                        String thumbFilaname = "";
103
                        if (isThumb) {
104
                            int dot = newFilename.lastIndexOf(".");
105
                            if ((dot >-1) && (dot < (newFilename.length()))) {
106
                                thumbFilaname = newFilename.substring(0, dot)+ "_thumb." + suffix; 
107
                                Thumbnails.of(absolutePath + newFilename).width(width).toFile(absolutePath + thumbFilaname);
108
                            }
109
                        }
110
111
                        return R.ok().put("picname", useUploadPath + newFilename).put("fileOldName", fileOldName).put("thumb", useUploadPath + thumbFilaname);
112
                    }
113
                } catch (Exception e) {
114
                    e.printStackTrace();
115
                    return R.error(e.getMessage());
77 116
                }
117
            } else {
118
                return R.error("上传的图片只允许是 png、 jpg、 webp 或 gif中的格式");
78 119
            }
79
            
80
            return R.ok(result);
81 120
        } else {
82
            return R.error("上传文件失败");
121
            return R.error("上传失败, 文件为空");
83 122
        }
84 123
    }
85 124
@ -376,6 +415,7 @@ public class AppFileManagerController {
376 415
                    file.transferTo(f); // 将上传的文件写入到系统中
377 416
                    result.put("status", 0);
378 417
                    result.put("filename", useUploadPath + newFilename);
418
                    result.put("fileOldName", fileOldName);
379 419
                }
380 420
            } catch (Exception e) {
381 421
                e.printStackTrace();

+ 2 - 2
src/main/resources/application.yml

@ -19,8 +19,8 @@ spring:
19 19
    date-format: yyyy-MM-dd HH:mm:ss
20 20
  servlet:
21 21
    multipart:
22
      max-file-size: 10MB
23
      max-request-size: 10MB
22
      max-file-size: 20MB
23
      max-request-size: 20MB
24 24
      enabled: true
25 25
  redis:
26 26
    open: false  # 是否开启redis缓存  true开启   false关闭