ソースを参照

pdf,doc,docx 上传

huwhois 5 年 前
コミット
be21cfaaa6
共有1 個のファイルを変更した48 個の追加1 個の削除を含む
  1. 48 1
      src/main/java/io/renren/modules/sys/controller/FileManagerController.java

+ 48 - 1
src/main/java/io/renren/modules/sys/controller/FileManagerController.java

@ -50,7 +50,7 @@ public class FileManagerController {
50 50
        // File createFile = new File(rootpath + "../data/upload/");
51 51
        String useUploadPath;   // 文件上传使用的目录
52 52
        String activePath = request.getParameter("activepath"); 
53
        if (activePath != null && activePath != "") {
53
        if (activePath != null && activePath.equals("")) {
54 54
            // 上传到指定目录
55 55
            useUploadPath = activePath + "/";
56 56
        } else {
@ -95,6 +95,53 @@ public class FileManagerController {
95 95
        }
96 96
    }
97 97
98
    @PostMapping("/uploadfile")
99
    public R uploadFile(@RequestParam(value = "upload_file") MultipartFile file,
100
     @RequestParam(value = "activepath") String activepath, @RequestParam(value = "useoldname") Integer useoldname) throws IOException {
101
        // 根据相对路径转化为真实路径
102
        // String rootpath = request.getSession().getServletContext().getRealPath(File.separator);// 获得web应用的绝对路径
103
        // File createFile = new File(rootpath + "../data/upload/");
104
        String useUploadPath;   // 文件上传使用的目录
105
        System.out.println(activepath);
106
        if (activepath != null && !activepath.equals("")) {
107
            // 上传到指定目录
108
            useUploadPath = activepath + "/";
109
        } else {
110
            // 使用配置文件中的指定上传目录下的 documents
111
            useUploadPath = uploadFolder  + "/documents/";
112
        }
113
114
        // System.out.println(useUploadPath);
115
        String picDir = dataPath + useUploadPath;   // 文件绝对路径
116
117
        File createFile = new File(picDir);
118
        if (!createFile.exists()) {// 判断文件是否存在如果不存在则自动创建文件夹
119
            createFile.mkdir();
120
        }
121
        // System.out.println(picDir);
122
        String fileOldName = file.getOriginalFilename();
123
        // 上传的图片只允许是 png、jpg 或 gif 中的格式
124
        if (fileOldName.contains(".pdf") || fileOldName.contains(".doc") || fileOldName.contains(".docx")) {
125
            String newFilename = "";
126
            if (useoldname == 1) {
127
                newFilename = fileOldName;
128
            } else {
129
                String uuid = UUID.randomUUID().toString().replace("-", "");// 随机生成一个唯一性的id 文件不重名
130
                String suffix = fileOldName.substring(fileOldName.lastIndexOf(".") + 1);
131
                newFilename = uuid + "." + suffix;
132
            }
133
            File f = new File(picDir + newFilename);
134
            if (f.exists()) {//上传的文件已经存在,则提示用户重新上传 apk 或者重命名
135
                return R.error("上传的文件已经存在,则提示用户重新上传 apk 或者重命名");
136
            } else {
137
                file.transferTo(f); // 将上传的文件写入到系统中
138
                return  R.ok().put("filename", useUploadPath + newFilename);
139
            }
140
        } else {
141
            return R.error("上传文件失败");
142
        }
143
    }
144
98 145
    /*
99 146
     * 读取指定路径下的文件名和目录名
100 147
     */