springboot实现上传和下载

1.添加pom依赖

        
            commons-fileupload
            commons-fileupload
            1.3.2
        
        
            commons-io
            commons-io
            2.5
        

2.页面代码




    
    文件上传


单文件上传

多文件上传

文件1:

文件2:

下载1 下载2

3.Controller代码

@Controller
public class FileController {

    private static final Logger log = LoggerFactory.getLogger(FileController.class);

    @ResponseBody
    @RequestMapping("upload")
    public String uload(MultipartFile file){
        String fileName = file.getOriginalFilename();
        byte[] bytes = new byte[0];
        try {
            bytes = file.getBytes();
            log.info("开始上传文件【{}】...", fileName);
            String path = "D:/test" ;
            File dest = new File(path + "/" + fileName);
            file.transferTo(dest);
//            FileUtils.writeByteArrayToFile(new File("D:/" + fileName), bytes);
        } catch (IOException e) {
            e.printStackTrace();
        }
        log.info("文件【{}】上传成功...", fileName);
        return "success";
    }
    @RequestMapping("download1")
    public void download1(HttpServletResponse response){
        String fileName = "954057adfc985fb958c44f5e2a4aa822.jpg";
        response.setHeader("content-type", "application/octet-stream");
        response.setContentType("application/octet-stream");
        response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
        byte[] buff = new byte[1024];
        BufferedInputStream bis = null;
        OutputStream os = null;
        try {
            os = response.getOutputStream();
            bis = new BufferedInputStream(new FileInputStream(new File("C:\\Users\\sss\\Pictures\\Saved Pictures\\"
                    + fileName)));
            int i = bis.read(buff);
            while (i != -1) {
                os.write(buff, 0, buff.length);
                os.flush();
                i = bis.read(buff);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bis != null) {
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    @RequestMapping("download2")
    public void download2(HttpServletResponse response){
        String fileName = "954057adfc985fb958c44f5e2a4aa822.jpg";
        // 设置强制下载不打开
        response.setContentType("application/force-download");
        try {
            response.setHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes("utf-8"),"ISO-8859-1"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
//        byte[] buff = new byte[1024];
        BufferedInputStream bis = null;
        try {
            ServletOutputStream outputStream = response.getOutputStream();
            byte[] buff = FileUtils.readFileToByteArray(new File("C:\\Users\\sss\\Pictures\\Saved Pictures\\" + fileName));
            outputStream.write(buff);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("batch")
    public String batchFileUpload(HttpServletRequest request){
        List fileList = ((MultipartHttpServletRequest) request).getFiles("file");
        for (int i = 0; i < fileList.size(); i++) {
            MultipartFile file = fileList.get(i);
            byte[] bytes = new byte[0];
            try {
                bytes = file.getBytes();
                log.info("开始上传文件【{}】...", i);
                FileUtils.writeByteArrayToFile(new File("D:/test/" + file.getOriginalFilename()), bytes);
                log.info("文件【{}】上传成功...", i);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return "success";
    }

}

4.application.properties文件配置

server.servlet.context-path=/file
server.port=8888
spring.mvc.view.suffix=.html
#文件上传设置
spring.servlet.multipart.max-file-size=100MB
spring.servlet.multipart.max-request-size=100MB

 

你可能感兴趣的:(maven)