package control; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import util.Info; public class Upload extends HttpServlet { /** * Constructor of the object. */ public Upload() { super(); } /** * Destruction of the servlet. <br> */ public void destroy() { super.destroy(); // Just puts "destroy" string in log // Put your code here } /** * The doGet method of the servlet. <br> * * This method is called when a form has its tag value method equals to get. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } /** * The doPost method of the servlet. <br> * * This method is called when a form has its tag value method equals to post. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { StringBuffer sb = new StringBuffer(50); response.setContentType("application/x-msdownload;charset=utf-8"); try { response.setHeader("Content-Disposition", new String(sb.toString() .getBytes(), "ISO8859-1")); } catch (UnsupportedEncodingException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } String filename = request.getParameter("filename"); if (request.getHeader("User-Agent").toLowerCase().indexOf("firefox") > 0){ try { filename = new String(filename.getBytes("UTF-8"), "ISO8859-1"); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } } else if (request.getHeader("User-Agent").toUpperCase().indexOf("MSIE") > 0){ try { filename = URLEncoder.encode(filename, "UTF-8"); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } } response.setContentType("text/plain"); response.setHeader("Location",filename); response.reset(); response.setHeader("Cache-Control", "max-age=0" ); response.setHeader("Content-Disposition", "attachment; filename=" + filename); try { BufferedInputStream bis = null; BufferedOutputStream bos = null; OutputStream fos = null; // File f = new File(request.getRealPath("/upfile/")+"/"+filename); //System.out.println(f); bis = new BufferedInputStream((InputStream)new FileInputStream(request.getRealPath("/upfile/")+"/"+filename)); fos = response.getOutputStream(); bos = new BufferedOutputStream(fos); int bytesRead = 0; byte[] buffer = new byte[5 * 1024]; while ((bytesRead = bis.read(buffer)) != -1) { bos.write(buffer, 0, bytesRead); } bos.close(); bis.close(); fos.close(); new Info().delPic(request.getRealPath("/upfile/")+"/", filename); } catch (Exception e) { e.printStackTrace(); }finally{ } } /** * Initialization of the servlet. <br> * * @throws ServletException if an error occurs */ public void init() throws ServletException { // Put your code here } }
最近下载更多
mmmmmmppp LV10
2022年12月31日
xudong_y LV4
2022年11月7日
文成1116 LV21
2022年10月25日
yijie110 LV5
2022年9月8日
zhaoyangwfd LV17
2022年6月21日
testuser1234567 LV24
2022年5月23日
2310573421 LV7
2022年4月23日
wanglinddad LV54
2022年3月28日
小五12345 LV13
2022年3月18日
edward_vic LV2
2022年3月9日
最近浏览更多
全栈小白 LV34
3月26日
fffffffanan LV1
3月2日
yenaever
1月4日
暂无贡献等级
xiaomii LV3
2023年12月12日
WBelong LV7
2023年12月11日
SovereignJet LV3
2023年10月29日
微信网友_6655384758161408
2023年9月18日
暂无贡献等级
wersdfs LV1
2023年9月14日
taoshen95 LV14
2023年8月23日
爱吃鱼的猫Vital LV6
2023年7月28日