Springboot - 파일 다운로드(백엔드)

3231 단어 ThymeleafSpringBoot
구성 파일:
// 
#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; } }

좋은 웹페이지 즐겨찾기