public ModelAndView download(HttpServletRequest request,HttpServletResponse response) throws Exception {
String fileName = request.getParameter("fileName");
String filePath = readProperties("report.path.prefix")+"/supplier/"+fileName;
File file = new File(filePath);
// 清空response
response.reset();
response.setHeader("Content-Disposition", "attachment; filename=\"" + new String(fileName.getBytes("gbk"),"iso-8859-1") + "\"");
response.addHeader("Content-Length", "" + file.length());
response.setContentType("application/octet-stream;charset=UTF-8");
InputStream in = null;
OutputStream toClient = null;
try{
//以流的形式下载文件
in = new BufferedInputStream(new FileInputStream(filePath));
byte[] buffer = new byte[in.available()];
in.read(buffer);
in.close();
toClient = new BufferedOutputStream(response.getOutputStream());
toClient.write(buffer);
toClient.flush();
}catch(Exception e){
e.printStackTrace();
}finally{
toClient.close();
}
return null;
}
public static Properties proper= null;
private String readProperties(String key){
if(this.proper==null){
InputStream in = this.getClass().getResourceAsStream("/env.properties");
try {
this.proper = new Properties();
this.proper.load(in);
} catch (IOException e) {
e.printStackTrace();
}
}
return this.proper.getProperty(key);
}
分享到:
相关推荐
- **流操作**:从文件系统或云存储读取文件内容,通过 `ServletOutputStream` 将内容写入响应流,发送到客户端。 - **处理中文文件名**:下载时需要注意处理中文文件名的编码问题,避免乱码。 3. **数据存储** -...
3. **文件路径问题**: 在处理文件时,确保你使用的文件路径是正确的,尤其是在多环境部署时要考虑相对路径和绝对路径的区别。 4. **编码问题**: 文件名可能包含非ASCII字符,需要正确处理编码,避免乱码。 5. **...
这段代码会将指定文件路径下的文件读取为字节数组,以便后续将其作为响应内容发送给客户端。 ```java import org.springframework.util.FileCopyUtils; public byte[] downloadFile(String fileName) { byte[] ...
**文件下载配置** 在Spring MVC中,为了支持文件下载,我们需要对`AnnotationMethodHandlerAdapter`进行配置。这涉及到设置`messageConverters`属性,以指定处理不同类型数据的转换器。例如,`...
- 可以,只要在启动类中通过`@ContextConfiguration`注解指定正确的配置文件路径即可。 **问题十一:Spring里面如何定义hibernatemapping?** - 通过配置Hibernate SessionFactory,并指定HBM文件的位置。 **问题...