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

Posted 是蓝蓝的呀

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了第六天-request response4-response实现文件下载.avi--本人测试失败相关的知识,希望对你有一定的参考价值。

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 response4-response实现文件下载.avi--本人测试失败的主要内容,如果未能解决你的问题,请参考以下文章

python爬虫第六天

[python]重新捡起来的第六天

[python]重新捡起来的第六天

javaSE第六天

第六天-整合

冲刺第六天