黑马程序员技术交流社区
标题:
一个关于ServletContext 下载的问题
[打印本页]
作者:
Crist
时间:
2013-12-20 16:53
标题:
一个关于ServletContext 下载的问题
package com.itheima.servlet;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//实现文件的下载
public class ServletContextDemo6Servlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//得到输入流:路径该怎么获取
String path = "/WEB-INF/2.jpg";// 斜线就代表当前应用
//告知客户端以下载的方式打开
//这个setHeader() 方法里面具体应该写什么
response.setHeader("Content-Disposition", "attachment;filename=2.jpg");
response.setHeader("Content-Type", "application/octet-stream");
String realPath = getServletContext().getRealPath(path);//根据相对于当前应用的路径,得到绝对路径
System.out.println(realPath);
InputStream in = new FileInputStream(realPath);
OutputStream out = response.getOutputStream();
int len = -1;
byte b[] = new byte[1024];
while((len=in.read(b))!=-1){
out.write(b, 0, len);
}
in.close();
out.close();
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
复制代码
//这个setHeader() 方法里面具体应该写什么
response.setHeader("Content-Disposition", "attachment;filename=2.jpg");
response.setHeader("Content-Type", "application/octet-stream");
作者:
25343215
时间:
2013-12-20 17:02
本帖最后由 25343215 于 2013-12-20 17:03 编辑
楼主少设置了
response.setContentType(getServletContext().getMimeType(filename));// 设置MIME类型
下面是一份可用的代码
在WebRoot里面创建目录response/demo6/,在这里放入文件。
然后通过动态访问的方法: ?文件名.格式 的方法连接。
package cn.itcast.servlet.response.demo6;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 实现文件下载
*
* @author seawind
*
*/
public class ResponseServlet6 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 获得文件名
String filename = request.getParameter("filename");
System.out.println(filename);
response.setContentType(getServletContext().getMimeType(filename));// 设置MIME类型
// tomcat/conf/web.xml
response.setHeader("Content-Disposition", "attachment;filename="
+ filename);
// 下载 读取目标文件,通过response 将目标文件内容写到客户端
// 绝对磁盘路径 读取文件
String fullFilename = getServletContext().getRealPath(
"/response/demo6/" + filename);
InputStream in = new FileInputStream(fullFilename);
OutputStream out = response.getOutputStream();
int b;
while ((b = in.read()) != -1) {
out.write(b);
}
in.close();
out.close();
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
复制代码
作者:
taxue0504
时间:
2013-12-20 17:03
{:soso_e129:}
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2