zoukankan      html  css  js  c++  java
  • 上传服务端

    package com.ch;

    import java.io.IOException; import java.io.PrintWriter;

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

    import java.io.DataInputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.PrintWriter;

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

    public class UploadFileServlet extends HttpServlet {

     /**   * Constructor of the object.   */  public UploadFileServlet() {   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 {      System.out.println("开始接收文件");   response.setCharacterEncoding("utf-8");   PrintWriter out = response.getWriter();         String fileName = request.getParameter("fileName");         String uploadPath = getServletContext().getRealPath("/upload");         //定义上载文件的最大字节           int MAX_SIZE = 102400 * 102400;                   //声明文件读入类           DataInputStream in = null;           FileOutputStream fileOut = null;                      //取得客户端上传的数据类型           String contentType = request.getContentType();                        if(contentType.indexOf("binary/octet-stream") >= 0){               //读入上传的数据               in = new DataInputStream(request.getInputStream());               //获得文件的大小             int formDataLength = request.getContentLength();               //  如果文件过大              if(formDataLength > MAX_SIZE){                   String errormsg=("1111111111111111上传的文件字节数不可以超过" + MAX_SIZE);                  out.println(errormsg);                  out.close();                 return ;              }            //保存上传文件的数据            byte[] dataBytes = new byte[formDataLength];            int byteRead = 0;            int totalBytesRead = 0;            //上传的数据保存在byte数组            while(formDataLength > totalBytesRead){             byteRead = in.read(dataBytes,totalBytesRead,formDataLength);             totalBytesRead += byteRead;            }            String filePath = uploadPath +"\"+ fileName;//得到文件保存路径          System.out.println("上传文件保存的路径:"+filePath);           //检查上传文件的目录是否存在            File fileDir = new File(uploadPath);            if(!fileDir.exists()){             fileDir.mkdirs();            }           //创建文件的写出类            fileOut = new FileOutputStream(filePath);            //保存文件的数据            fileOut.write(dataBytes);            fileOut.flush();          fileOut.close();         out.println("222222222222上传成功");    out.flush();    out.close();         }  }

     /**   * 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 {

      doGet(request, response);  }

     /**   * Initialization of the servlet. <br>   *   * @throws ServletException if an error occurs   */  public void init() throws ServletException {   // Put your code here  }

    }

  • 相关阅读:
    Spring MVC 复习笔记03
    Spring MVC 复习笔记02
    CSS之选择器
    Filedset
    Label标签
    Table标签
    列表标签
    CSS之img标签
    CSS之a标签锚点
    CSS之checkbox&radio&textarea&select
  • 原文地址:https://www.cnblogs.com/liuliwei123456/p/5436242.html
Copyright © 2011-2022 走看看