博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
上传服务端
阅读量:5896 次
发布时间:2019-06-19

本文共 3355 字,大约阅读时间需要 11 分钟。

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  }

}

转载于:https://www.cnblogs.com/liuliwei123456/p/5436242.html

你可能感兴趣的文章
windows server2008多用户远程登陆设置方法
查看>>
sencha touch巧妙使用请求超时提升用户体验
查看>>
15. 3Sum
查看>>
26. Remove Duplicates from Sorted Array
查看>>
在使用AngularJS的过程中了解Promise(二)
查看>>
ArrayList源码解析
查看>>
基于SpringMVC、Maven以及Mybatis的环境搭建
查看>>
可见面判别算法---区域细分算法
查看>>
清理恢复文本框的默认值
查看>>
【原创】如何在vim中使用tab进行python代码补全
查看>>
Struts秘籍之起式:第1.3式:迁移至Struts 1.1
查看>>
绿色PLSQL/Developer搭配Oracle精简客户端使用
查看>>
ViewPager Banner(广告墙)
查看>>
Spring Cloud 入门教程(二): 服务消费者(rest+ribbon)(Greenwich.RELEASE)
查看>>
iOS开发20:Navigation Bar的简单设置
查看>>
iOS开发24:使用SQLite3存储和读取数据
查看>>
GMF树形布局 2 实现展开/折叠
查看>>
Cocos2dx 2.0x Touch事件
查看>>
php判断是否登录
查看>>
Yii2 Unable to verify your data submission 错误-CSRF
查看>>