jsp实现文件下载
web.xml
<mime-mapping>
<extension>doc</extension>
<mime-type>application/vnd.ms-word</mime-type>
</mime-mapping>
download.jsp
<%@page language="java" contentType="application/x-msdownload" import="java.io.*,java.net.*" pageEncoding="UTF-8"%><%
//关于文件下载时采用文件流输出的方式处理:
//加上response.reset(),并且所有的%>后面不要换行,包括最后一个;
//因为Application Server在处理编译jsp时对于%>和<%之间的内容一般是原样输出,而且默认是PrintWriter,
//而你却要进行流输出:ServletOutputStream,这样做相当于试图在Servlet中使用两种输出机制,
//就会发生:getOutputStream() has already been called for this response的错误
//详细请见《More Java Pitfill》一书的第二部分 Web层Item 33:试图在Servlet中使用两种输出机制 270
//而且如果有换行,对于文本文件没有什么问题,但是对于其它格式,比如AutoCAD、Word、Excel等文件
//下载下来的文件中就会多出一些换行符0x0d和0x0a,这样可能导致某些格式的文件无法打开,有些也可以正常打开。
response.reset();//可以加也可以不加
response.setContentType("application/x-download");//1、unknown ==> application/x-download
//../../退WEB-INF/classes两级到应用的根目录下去
String filenamedownload ="F:/软件/eclipse-jee-indigo-SR2-win32.zip";
File f = new File(filenamedownload);
if(f.exists()){
System.out.println(">>>");
}
System.out.println("路径是"+filenamedownload);
String filenamedisplay = "系统eclipse-jee-indigo-SR2-win32.zip";//系统解决方案.txt
// if (request.getHeader("User-Agent").toLowerCase().indexOf("firefox") > 0){
System.out.print(">>");
String filename = new String(filenamedisplay.getBytes("UTF-8"), "ISO8859-1");//firefox浏览器
filenamedisplay=filename;
request.setCharacterEncoding("UTF-8"); //设置输入编码格式
response.setContentType("text/html;charset=UTF-8"); //设置输出编码格式
response.addHeader("Content-Disposition","attachment;filename=" + filenamedisplay);
//路径是/E:/MyEclipse项目/.metadata/.me_tcat/webapps/PartTimeService/WEB-INF/classes/../../系统解决方案.doc
OutputStream output = null;
FileInputStream fis = null;
try
{
output = response.getOutputStream();
fis = new FileInputStream(filenamedownload);
byte[] b = new byte[1024*10];
int i = 0;
while((i = fis.read(b)) > 0)
{
output.write(b, 0, i);
}
output.flush();
}
catch(Exception e)
{
System.out.println("Error!");
e.printStackTrace();
}
finally
{
if(fis != null)
{
fis.close();
fis = null;
}
if(output != null)
{
output.close();
output = null;
}
}
%>
new Thread(new Runnable() {
public void run() {
String path="http://192.168.1.108:8090/PartTimeService/down.jsp";
try {
InputStream is = Get_inputStream(path);
WriteFile(is);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start();
}
/**
* 获取下载文件的InputStream对象
*
* @param urlStr
* 下载文件的地址
* @return 返回InputStream文件对象
* @throws MalformedURLException
* @throws IOException
*/
public InputStream Get_inputStream(String urlStr)
throws MalformedURLException, IOException {
// 创建按一个URL实例
URL url = new URL(urlStr);
// 创建一个HttpURLConnection的链接对象
HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
httpConn.setDoOutput(true);
httpConn.setRequestMethod("POST");
httpConn.setRequestProperty("contentType", "GBK");
httpConn.connect();
String k = httpConn.getContentType();
System.out.println(k);
string = new String(httpConn.getHeaderField("Content-Disposition").getBytes("ISO8859_1"), "utf-8"); //获取服务器文件名加上转码
Map<String,List<String>> ml= httpConn.getHeaderFields(); //
// 获取所下载文件的InputStream对象
InputStream inputStream = httpConn.getInputStream();
// 返回下载文件的InputStream对象
return inputStream;
}
/**
* 写入文件
* @param inputStream 下载文件的字节流对象
* @param sdpath 文件的存放目录
*/
public void WriteFile( InputStream inputStream) {
try {
//在指定目录创建一个空文件并获取文件对象
// File file = createSDFile(sdpath);
//获取一个写入文件流对象
OutputStream ouput =new FileOutputStream(Environment.getExternalStorageDirectory()+"/apks/"+string+".jpg");
//创建一个4*1024大小的字节数组,作为循环读取字节流的临时存储空
byte buffer[] = new byte[4*1024];
//循环读取下载的文件到buffer对象数组中
int len = 0;
while((len=inputStream.read(buffer)) !=-1) {
//把文件写入到文件
Log.e("eee",">>"+len);
ouput.write(buffer,0,len);
}
ouput.flush();
ouput.close();
} catch (Exception e) {
// TODO: handleexception
e.printStackTrace();
}finally{
try {
//关闭写入流
} catch (Exception e){
// TODO:handle exception
e.printStackTrace();
}
}
}