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
	}
}
最近下载更多
                
                cherrylxj     LV3
                2024年6月13日
            
            
        
                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     LV55
                2022年3月28日
            
            
        
                小五12345     LV13
                2022年3月18日
            
            
        
最近浏览更多
                
                vincemokea     LV9
                9月14日
            
            
        
                630328293l     LV2
                3月5日
            
            
        
                lyt010628     LV4
                2024年7月9日
            
            
        
                柳咪华沙     LV7
                2024年6月29日
            
            
        
                cherrylxj     LV3
                2024年6月12日
            
            
        
                hfffff     LV1
                2024年6月3日
            
            
        
                zeyang_wang     LV8
                2024年5月30日
            
            
        
                sunlea     LV20
                2024年5月23日
            
            
        
                云空城     LV1
                2024年5月22日
            
            
        
                2602275348     LV12
                2024年5月16日
            
            
        
                
                
                