欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

spring mvc 下载文件 博客分类: Java Web spring mvcspring mvc下载文件spring downloadspring mvc downloadspring下载文件 

程序员文章站 2024-02-24 12:40:58
...

spring mvc如何下载文件呢?

spring mvc怎么下载图片呢?

有两种方式:

方式一:使用注解@ResponseBody

 

@ResponseBody
	@RequestMapping(value = "/download",produces="image/jpeg")
	public byte[] downloadFile(HttpServletRequest request, HttpServletResponse response,String contentType2,boolean isInline)
			throws IOException {
		byte[]bytes=FileUtils.getBytes4File("D:\\Temp\\cc.jpg");
//		response.addHeader("Content-Disposition", downloadType+";filename=\"a.jpg\"");
		WebServletUtil.setDownloadContentDisposition(isInline, "c.jpg", response);
		return bytes;

	}

 WebServletUtil.setDownloadContentDisposition 的实现如下:

 

 

/***
	 * spring MVC下载文件设置的Content-Disposition
	 * @param isInline
	 * @param fileName
	 * @return
	 */
	public static String getContentDisposition(boolean isInline,String fileName){
		String downloadType=null;
		if(isInline){
			downloadType=Constant2.CONTENT_DISPOSITION_INLINE;
		}else{
			downloadType=Constant2.CONTENT_DISPOSITION_ATTACHMENT;
		}
		if(ValueWidget.isNullOrEmpty(fileName)){
			fileName="name_not_specified";
		}
		String format=downloadType+";filename=\""+fileName+"\"";
		return format;
	}
	/***
	 * 下载文件(或内联显示)时设置Content-Disposition
	 * @param isInline
	 * @param fileName
	 * @param response
	 */
	public static void setDownloadContentDisposition(boolean isInline,String fileName, HttpServletResponse response){
		response.addHeader(Constant2.CONTENT_DISPOSITION, WebServletUtil.getContentDisposition(isInline, fileName));
	}

 注意:(1)一定要通过@RequestMapping注解的produces 设置response 的content type;

(2)设置应答头时要使用addHeader,而不是setHeader

 

方式二:使用ResponseEntity

@RequestMapping(value = "/download3")
    public ResponseEntity<byte[]> download() throws IOException {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.IMAGE_JPEG);
//        headers.setContentDispositionFormData("inline", "dict.jpg");//attachment
        headers.set(Constant2.CONTENT_DISPOSITION,WebServletUtil.getContentDisposition(true, "dict.jpg"));
        return new ResponseEntity<byte[]>(FileUtils.getBytes4File("D:\\Temp\\cc.jpg"),
                                          headers, HttpStatus.CREATED);
    }

/***
	 * favicon.ico 
	 * @throws IOException 
	 */
	@RequestMapping(value = "/favicon.ico")
	public ResponseEntity<byte[]> faviconIco(HttpServletRequest request) throws IOException {
		HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.IMAGE_PNG);
        String faviconIcoName="sms-4.ico";
        headers.set(Constant2.CONTENT_DISPOSITION,WebServletUtil.getContentDisposition(true, faviconIcoName));
        ///home/whuang/software/apache-tomcat-7.0.53/webapps/ROOT/
        String webappPath=null;
        if(WebServletUtil.isLocalIp(request)){//服务器在本机(访问ip为127或localhost)
        	webappPath=WebServletUtil.getRealPath(request);
        }else{
        	webappPath=DictionaryParam.get(Constant2.DICTIONARY_GROUP_GLOBAL_SETTING, "WEB-INF_LOC");
        }
        return new ResponseEntity<byte[]>(FileUtils.getBytes4File(
        		webappPath
        		+"WEB-INF/static/img/"+faviconIcoName),
                                          headers, HttpStatus.CREATED);

	}

  

注意:不要使用headers.setContentDispositionFormData 来设置Content-Disposition