java利用dropzone多文件上傳

@RequestMapping("/upload_file_json")
public Object upload_media(StandardMultipartHttpServletRequest req, HttpServletResponse rsp) {
try {
String dirName = Objects.toString(req.getParameter("dir"), "image");
Subject subject = SecurityUtils.getSubject();
Session session = subject.getSession();
String currCompany = (String) session.getAttribute("currCompany");
if(AppUtils.isBlank(currCompany)){
ModelAndView errorMv = new ModelAndView("/redirect:/403");
errorMv.addObject("message", "缺少公司慘數(shù)");
return errorMv;
}
//文件保存目錄路徑
//rootPath/dirPath/dirName/ymdPath/fileName
String ymdPath = DateUtils.formatDate(new Date(), "yyyy_MM_dd") + "/";
String dirPath = "/" + currCompany + "/";
String rootPath = FileConfigUtils.getImagesSavePath();
String os = System.getProperty("os.name");
if (os.toLowerCase().startsWith("linux")) {
//rootPath = SystemManager.inst().upload_path;
}
String filePath = dirPath + dirName + "/" + ymdPath;
/新文件名字的命名規(guī)則*/
Function<String, String> nameFunc = fileName -> {
String fileExt = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();//檢查後綴名
String newFileName = IDManager.generateID();
return newFileName + "." + fileExt;
};

        rsp.setContentType("text/html; charset=UTF-8");

        if (!ServletFileUpload.isMultipartContent(req)) {
            return getError("請選擇文件。");
        }

        //檢查目錄
        File uploadDir = new File(rootPath);
        //檢查目錄
       /* if(!uploadDir.isDirectory()){
            System.out.println("上傳目錄不存在。");
            return getError("上傳目錄不存在.");
        }*/
        //檢查目錄寫權(quán)限
        if (!uploadDir.canWrite()) {
            System.out.println("上傳目錄沒有寫權(quán)限。");
            return getError("上傳目錄沒有寫權(quán)限。");
        }

        List<String> url = upload_multi_file(rootPath, filePath, nameFunc, req);
        System.out.println("************富文本框上傳圖片地址************" + url);
        JSONObject ret = new JSONObject();
        ret.put("error", 0);
        ret.put("url", url);

        return ret;
    } catch (Exception e) {
        e.printStackTrace();
        return getError("未知錯(cuò)誤");
    }
}

private String getError(String message) {
    JSONObject ret = new JSONObject();
    ret.put("error", 1);
    ret.put("url", message);
    return ret.toJSONString();
}

public List<String> upload_multi_file(String saveRootPath, String filePath, Function<String, String> nameFunc, StandardMultipartHttpServletRequest freq) {
    StopWatch stopWatch = new StopWatch("upfile");
    stopWatch.start("save file");

    String saveUrl = "/" + filePath;
    String realpath = saveRootPath + filePath;
    File dir = new File(realpath);
    System.out.println("exists = "+dir.exists());
    if (!dir.exists()) {
        dir.mkdirs();
    }

    //String file_url[] = new String[1];
    List<String> urlList = new ArrayList<>();
    try {
        freq.getMultiFileMap().values().stream().forEach(files -> {
            files.stream().forEach((MultipartFile file) -> {
                try {
                    String fileName = file.getOriginalFilename();
                    if (AppUtils.isBlank(fileName)) {
                        System.out.println("upload failed because of nofiles fileName=" + fileName);
                        return;
                    }
                    String _new_file_original_name = fileName;
                    if (null != nameFunc) {
                        _new_file_original_name = nameFunc.apply(fileName);
                    } else {
                        _new_file_original_name = fileName;
                    }
                    File newFile = new File(dir, _new_file_original_name);
                    file.transferTo(newFile);
                    //file_url[0] = saveUrl + newFile.getName();
                    urlList.add(FileConfigUtils.getImagesServerUrl()+saveUrl + newFile.getName());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            });
        });
    } catch (Exception e2) {
        e2.printStackTrace();
        return urlList;
    }
    stopWatch.stop();
    return urlList;
}

}
————————————————
版權(quán)聲明:本文為CSDN博主「choiHingLeung」的原創(chuàng)文章,遵循 CC 4.0 BY-SA 版權(quán)協(xié)議,轉(zhuǎn)載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/u011562082/article/details/87256083

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容