码迷,mamicode.com
首页 > 其他好文 > 详细

第六天-request response\04-response实现文件下载.avi--本人测试失败

时间:2017-05-22 13:22:45      阅读:226      评论:0      收藏:0      [点我收藏+]

标签:bst   filename   pen   uid   tps   todo   vax   context   html   

package cn.itcast.response;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* Servlet implementation class ResponseDemo1
*/
@WebServlet("/ResponseDemo1")
public class ResponseDemo1 extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
* @see HttpServlet#HttpServlet()
*/
public ResponseDemo1() {
super();
// TODO Auto-generated constructor stub
}

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String path = this.getServletContext().getRealPath("/download/abc.jpg");
String filename =path.substring(path.lastIndexOf("\\"+1));
//response.setHeader("content-type", "text/html;charset=UTF-8");
response.setHeader("content-disposition", "attachment;filename="+filename);
InputStream in =null;
OutputStream out =null;
try {
in = new FileInputStream(path);
int len =0;
byte buffer[] =new byte[1024];
out =response.getOutputStream();
while ((len=in.read(buffer))>0){
out.write(buffer,0,len);
}
} finally {
// TODO: handle finally clause
}


//test1(request, response);
}

public void test1(HttpServletRequest request, HttpServletResponse response) throws IOException {
response.setCharacterEncoding("UTF-8");
response.setHeader("content-type", "text/html;charset=UTF-8");
String data="中国";

//response.setContentType("text/html;charset=UTF-8");
PrintWriter out= response.getWriter();
out.write(data);
response.getWriter().append("Served at: ").append(request.getContextPath()+"fda "+new Date());
}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}

}

第六天-request response\04-response实现文件下载.avi--本人测试失败

标签:bst   filename   pen   uid   tps   todo   vax   context   html   

原文地址:http://www.cnblogs.com/rogge7/p/6888784.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!