package com.tiglle.servlet;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class FileServlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("application/octet-stream");
resp.setHeader("Content-Disposition", "attachment;filename=test.mp4");
File file = new File("C:\\test.txt");
FileInputStream fis = new FileInputStream(file);
byte[] b = new byte[1024];
int len = 0;
while((len=fis.read(b))!=-1){
resp.getOutputStream().write(b, 0, len);
}
fis.close();
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
super.doPost(req, resp);
}
}
javaWeb的servlet读取服务器文件并下载到浏览器
最新推荐文章于 2024-07-10 02:44:02 发布