Springboot-文件下载(后端)

配置文件:

//服务器路径
#file.uploadFolder=/root/uploadFiles/
//本地路径
file.uploadFolder=d://uploadFiles/
 
//上传文件大小和总量限制
spring.servlet.multipart.max-file-size=50Mb
spring.servlet.multipart.max-request-size=50Mb

 

Controller:

@Controller
@RequestMapping("/file")
public class FileController {
    @Autowired
    private HttpServletRequest request;
    
    @Autowired
    private FileService fileService;
    
    @Value("${file.uploadFolder}")
    private String uploadFolder;

    
    // 文件下载相关代码
    @RequestMapping("/downfile/{id}")
    public String downloadFile(HttpServletRequest request, HttpServletResponse response, @PathVariable String id) throws Exception {
	FileSource filesource = fileService.findbyid(id);
		
	String fileName = filesource.getFilename();// 设置文件名,根据业务需要替换成要下载的文件名

        if (fileName != null) {
            //设置文件路径
        	String realPath = uploadFolder;
        	File file = new File(realPath , fileName);
        	if (file.exists()) {
        		response.setContentType("application/force-download");// 设置强制下载不打开
        		//response.addHeader("Content-Disposition", "attachment;fileName=" + fileName);// 设置文件名
        		//response.setContentType("multipart/form-data;charset=UTF-8");也可以明确的设置一下UTF-8,测试中不设置也可以。 
        		response.setHeader("Content-Disposition", "attachment;fileName="+ new String(fileName.getBytes("GB2312"),"ISO-8859-1"));  
        		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);
                    }
                    System.out.println("下载成功");
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                	if (bis != null) {
                        try {
                            bis.close();
                        } catch (IOException e) {
                        	e.printStackTrace();
                        }
                	}
                	 if (fis != null) {
                         try {
                             fis.close();
                         } catch (IOException e) {
                             e.printStackTrace();
                         }
                     }
                 }
             }
         }
         return null;
     }

 

FileServiceImpl:

@Service
public class FileServiceImpl implements FileService {
 
     @Autowired
     private FileRepository fileRepository;
	
     /**
     * 查找多媒体 
     * 

查找多媒体

* @author MengMeng * @param id 多媒体ID * @Date Created date: 2018/10/25 * @return FileSource */ @Override public FileSource findbyid(String id) { // TODO Auto-generated method stub FileSource filesource = fileRepository.findByFileId(id); return filesource; } }

 

你可能感兴趣的:(Thymeleaf,SpringBoot)