不废话,贴代码
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
OutputStream out = null; // 输出响应正文的输出流
InputStream in = null; // 读取本地文件的输入流
// 获取filename请求参数
String filename = request.getParameter("filename");
String filepath = request.getParameter("filepath");
if (null == filename) {
out = response.getOutputStream();
out.write("Please input filename.".getBytes());
out.close();
return;
}
filename=new String(filename.getBytes("ISO-8859-1"),"UTF-8");
UploadConfiguration cfg = UploadConfiguration.getConfiguration();
String realPath = cfg.getPhysicalPath(filepath);
// 获得读取本地文件的输入流
in = new FileInputStream(new File(realPath));
int length = in.available();
// 设置响应正文的MIME类型
response.setContentType("application/force-download");
response.setHeader("Content-length", String.valueOf(length));
response.setHeader("Content-Disposition", "attachment;filename=\"" + BumfUtils.toUtf8String(filename) + "\" ");
response.setHeader("ContentType","text/html;charset=UTF-8");
// 把本地文件中的数据发送给客户
out = response.getOutputStream();
int bytesRead = 0;
byte[] buffer = new byte[512];
while ((bytesRead = in.read(buffer)) != -1) {
out.write(buffer, 0, bytesRead);
}
in.close();
out.close();
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
OutputStream out = null; // 输出响应正文的输出流
InputStream in = null; // 读取本地文件的输入流
// 获取filename请求参数
String filename = request.getParameter("filename");
String filepath = request.getParameter("filepath");
if (null == filename) {
out = response.getOutputStream();
out.write("Please input filename.".getBytes());
out.close();
return;
}
filename=new String(filename.getBytes("ISO-8859-1"),"UTF-8");
UploadConfiguration cfg = UploadConfiguration.getConfiguration();
String realPath = cfg.getPhysicalPath(filepath);
// 获得读取本地文件的输入流
in = new FileInputStream(new File(realPath));
int length = in.available();
// 设置响应正文的MIME类型
response.setContentType("application/force-download");
response.setHeader("Content-length", String.valueOf(length));
response.setHeader("Content-Disposition", "attachment;filename=\"" + BumfUtils.toUtf8String(filename) + "\" ");
response.setHeader("ContentType","text/html;charset=UTF-8");
// 把本地文件中的数据发送给客户
out = response.getOutputStream();
int bytesRead = 0;
byte[] buffer = new byte[512];
while ((bytesRead = in.read(buffer)) != -1) {
out.write(buffer, 0, bytesRead);
}
in.close();
out.close();
}