1. 程式人生 > >Springboot-檔案下載(後端)

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;
	
     /**
     * <b>查詢多媒體 </b>
     * <p>查詢多媒體</p>
     * @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;
     }
}