2
13693261870
2022-09-16 653761a31dfeb50dd3d007e892d69c90bf0cdafc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package com.landtool.lanbase.modules.res.controller;
 
import java.io.File;
 
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
 
import com.landtool.lanbase.config.SysTemPropertyConfig;
import com.landtool.lanbase.modules.sys.controller.AbstractController;
 
/**
 * 出图模板上传服务
 *
 * @author wq
 */
@Controller
@RequestMapping("/api/restemplate")
public class UploadTemplateController extends AbstractController {
    @Autowired
    private SysTemPropertyConfig sysConfig;
 
    /**
     * 模板上传
     *
     * @param file
     * @return
     */
    @ResponseBody
    @RequestMapping("/uploadfile")
    public String uploadfile(@RequestParam("file") MultipartFile file, String rtid, String rtname) {
        String message = "";
        if (!file.isEmpty()) {
            // 上传目录
            String thisPath = sysConfig.getUploadTemplatesPath();
            String paths[] = thisPath.split("\\\\");
 
            //创建文件夹
            String dir = paths[0];
            for (int i = 0; i < paths.length - 1; i++) {
                try {
                    dir = dir + "/" + paths[i + 1];
                    File dirFile = new File(dir);
                    if (!dirFile.exists()) {
                        dirFile.mkdirs();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            //判断文件是否存在
            String fileName = file.getOriginalFilename();
            File thisfile = new File(thisPath + "\\" + fileName);
            if (rtid != null && !"".equals(rtid)) {//编辑更新状态,删除之前上传模板
                File fi = new File(thisPath + "\\" + rtname + "." + file.getOriginalFilename().split("\\.")[1]);
                fi.delete();
            }
            //修改文件名
            for(int i=1;i<100;i++){
                if (thisfile.exists()) {
                    fileName = file.getOriginalFilename().split("\\.")[0] + "("+i+")." + file.getOriginalFilename().split("\\.")[1];
                    thisfile = new File(thisPath + "\\" + fileName);
                }else{
                    break;
                }
            }
            
//            String webpath = "template/" + fileName;
            try {
                FileUtils.copyInputStreamToFile(file.getInputStream(), thisfile);
//              message = "{'result':'1','name':'" + fileName + "','Path':'" + sysConfig.getUploadRootPath() + webpath + "'}";
                message = "{'result':'1','name':'" + fileName + "','Path':'上传成功!'}";
            } catch (Exception e) {
                e.printStackTrace();
                message = "{'result':'0'}";
            }
        }
        return message;
    }
    
    @RequestMapping("/deletefile")
    public void deletefile(String rtname){
        System.out.println(rtname);
        // 文件目录
        String filePath = sysConfig.getUploadTemplatesPath();
        File file = new File(filePath+"\\"+rtname+".mxd");
        if(file.exists()){
            file.delete();            
        }
    }
}