您的位置:首页 > 编程语言 > Java开发

SpringBoot单文件和多文件上传,通用的文件上传方法

2019-09-25 07:08 1161 查看
//通用的文件上传方法
public String saveFileByIO(MultipartFile upload, String fileSeparator, String uploadPath, int flag) {
        if (upload == null) {
                return "";
        }
        InputStream in = null;
        try {
                in = upload.getInputStream();
        } catch (IOException e2) {
                log.debug(e2);

        }
        File dir = new File(uploadPath);
        if (!dir.exists()) {
                dir.mkdirs();
        }
        String fileName = upload.getOriginalFilename();
        if (flag == 1) {
                String uuidString = UUID.randomUUID().toString();
                String suffix = fileName.substring(fileName.indexOf("."));
                fileName = uuidString + (suffix != null ? suffix : "");
        }
        String relaPth = uploadPath + fileSeparator + fileName;
        File checkName = new File(relaPth);
        String reName = fileName;
        if (checkName.exists()) {
                String suffix = fileName.substring(fileName.indexOf("."));
                reName = fileName.substring(0, fileName.indexOf(".")) + "_" + DateUtil.getTodayTimeString().replace(" ", "_").replace(":", "-") + (suffix != null ? suffix : "");
                relaPth = uploadPath + fileSeparator + reName;
        }
        try {
                upload.transferTo(new File(relaPth));
        } catch (IllegalStateException e1) {
                log.debug(e1);
        } catch (IOException e1) {
                log.debug(e1);
        }
        return reName;
}

单文件上传

public String upload(@RequestParam("file") MultipartFile file) {
try {
if (file == null || file.isEmpty()) {
return "文件为空";
}
// 获取文件名
String fileName = file.getOriginalFilename();
log.info("上传的文件名为:" + fileName);
// 设置文件存储路径
SimpleDateFormat sdf = new SimpleDateFormat("yyyy" + "/" + "MM" + "/" + "dd");
String subPath = sdf.format(new Date());
String basePath = subPath + "/" + fileName;
String rootPath = userFileClient.findConfigByCode("UPLOAD_ROOT_PATH").toString();
String path = rootPath + basePath;
File dest = new File(path);
// 检测是否存在目录
if (!dest.getParentFile().exists()) {
dest.getParentFile().mkdirs();// 新建文件夹
}
file.transferTo(dest);// 文件写入
fileinfo = saveFileMessage(file, path);
insert(fileinfo);
return "上传成功";
} catch (IllegalStateException e) {
log.error(e);
} catch (IOException e) {
log.error(e);
}
}

多文件上传

public String handleFileUpload(HttpServletRequest request) {
List<MultipartFile> files = ((MultipartHttpServletRequest) request).getFiles("file");
MultipartFile file = null;
BufferedOutputStream stream = null;
for (int i = 0; i < files.size(); ++i) {
file = files.get(i);
if (!file.isEmpty()) {
try {
byte[] bytes = file.getBytes();
String fileName = file.getOriginalFilename();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy" + "/" + "MM" + "/" + "dd");
String subPath = sdf.format(new Date());
String basePath = subPath + "/" + fileName;
String rootPath = userFileClient.findConfigByCode("UPLOAD_ROOT_PATH").toString();
String path = rootPath + basePath;
stream = new BufferedOutputStream(new FileOutputStream(
new File(path)));//设置文件路径及名字
stream.write(bytes);// 写入
fileinfo = saveFileMessage(file, path);
insert(fileinfo);
stream.close();
} catch (Exception e) {
stream = null;
return "第 " + i + " 个文件上传失败 ==> "
+ e.getMessage();
}
} else {
return "第 " + i
+ " 个文件上传失败因为文件为空";
}
}
return "上传成功";
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Spring Spring Boot