spring mvc 下载文件

Java 发表评论

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

发表评论

电子邮件地址不会被公开。 必填项已用*标注

昵称 *