您的位置:首页 > 编程语言 > Java开发

spring mvc 文件的下载

2014-01-02 15:17 381 查看
1.配置文件

<bean id="multipartResolver"  class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- value值为-1,不限制文件上传大小 -->
<property name='maxUploadSize'>
<value>-1</value>
</property>
</bean>


2.java代码

try{
path = request.getParameter("FileName")==null ? "":request.getParameter("FileName");
File file = new File(path);
if (file.exists())
{
InputStream fis = new FileInputStream(file);
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
response.setContentType("application/x-msdownload");
response.setContentLength((int) file.length());
response.setHeader("Content-Disposition","attachment;filename="+ name);
bis = new BufferedInputStream(fis);
bos = new BufferedOutputStream(response.getOutputStream());
byte[] buff = new byte[2048];
int bytesRead;
while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff, 0, bytesRead);
}
fis.close();
bis.close();
bos.close();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: