springmvc下载文件

@RequestMapping("download")
public ResponseEntity<byte[]> download() {
       HttpHeaders headers = new HttpHeaders();
       headers.setContentDispositionFormData("attachment", filename);
       headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);  
       headers.setContentLength(file.length());
       return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file), headers, HttpStatus.CREATED);
}
 
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">  
    <property name="messageConverters">  
        <list>  
            //把ByteArray加在Json前面  
            <bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter"/>  
            <bean id="jsonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" >  
                <property name = "supportedMediaTypes">  
                    <list>  
                        <value>text/plain;charset=UTF-8</value>  
                    </list>  
                </property>  
            </bean>  
        </list>  
    </property> 
</bean>  
提示:对于大的内容10MB +它设置contentLength是否重要。如果你错过了 浏览器将不能正常下载。 (确切的文件大小高达其作品,未经contentLength是否浏览器时效关系)

你可能感兴趣的:(springMVC)