SpringBoot单文件&&多文件上传

@RestController
public class FileController {

/**
 * 单文件上传
 *
 * @param file
 * @return
 */
@RequestMapping("/upload")
public String upload(@RequestParam("file") MultipartFile file) {
    try {
        if (file.isEmpty()) {
            return "file is empty!";
        }
        String fileName = file.getOriginalFilename();
        String suffixName = fileName.substring(fileName.lastIndexOf("."));
        //设置文件存储路径
        String filePath = "D:\\program\\springfile";
        String path = filePath + File.separator + fileName;
        File dest = new File(path);
        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
        }
        file.transferTo(dest);
        return "update success!";
    } catch (IOException e) {
        e.printStackTrace();
    }
    return "update failure!";
}

/**
 * 多文件上传
 */
@PostMapping("/batch")
public String handleFileUpdate(HttpServletRequest request) {
    List files = ((MultipartHttpServletRequest) request).getFiles("file");

    MultipartFile file = null;
    BufferedOutputStream stream;
    String filePath = "D:\\program\\springfile";
    for (MultipartFile multipartFile : files) {
        if (!multipartFile.isEmpty()) {
            try {
                byte[] bytes = file.getBytes();
                stream = new BufferedOutputStream(
                        new FileOutputStream(
                                new File(filePath + File.separator + multipartFile.getOriginalFilename()
                                )
                        )
                );
                stream.write(bytes);//写入
                stream.close();//关闭
            } catch (IOException e) {
                e.printStackTrace();
                return multipartFile.getOriginalFilename() + "上传失败!";
            }
        } else {
            return multipartFile.getOriginalFilename() + "上传失败!";
        }
    }

    return "update files success!";
}

@GetMapping("/download")
public String downloadFile(HttpServletRequest request, HttpServletResponse response) throws IOException {
    String fileName = "aaa.txt";
    if (fileName != null) {
        String filePath = "D:\\program\\springfile";
        File file = new File(filePath + File.separator + fileName);
        if (file.exists()) {
            response.setContentType("application/force-download");
            response.addHeader("Content-Disposition", "attachment;fileName=" + fileName);
            byte[] buffer = new byte[1024];
            FileInputStream fis = null;
            BufferedInputStream bis = null;
            try {
                fis = new FileInputStream(file);
                bis = new BufferedInputStream(fis);
                OutputStream os = response.getOutputStream();
                int i = bis.read(buffer);
                while (i != -1) {
                    os.write(buffer, 0, i);
                    i = bis.read(buffer);
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (bis != null)
                    bis.close();
                if (fis != null)
                    fis.close();
            }
            return "download success!";
        }
    }
    return "failure";
}

}

你可能感兴趣的:(Java)