zoukankan      html  css  js  c++  java
  • 功能:文件上传

    public class UoloadServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    //1.创建文件磁盘工厂
    DiskFileItemFactory diskFileItemFactory = new DiskFileItemFactory();
    //2.使用文件磁盘工厂创建文件上传对象
    FileUpload fileUpload = new FileUpload(diskFileItemFactory);
    //3.使用文件上传对象的parse方法 去解析请求数据流
    try {
    List<FileItem> lists = fileUpload.parseRequest(request);
    for (FileItem fileItem : lists) {
    String fieldName = fileItem.getFieldName();
    if(fileItem.isFormField()){
    //此项是普通输入框
    //getFieldName代表的是参数名
    //返回参数值
    String value = fileItem.getString("utf-8");
    System.out.println("参数名:"+fieldName+"-参数值:"+value);
    }else{
    //文件上传类型
    //获取文件名
    String name = fileItem.getName();//
    //文件输入流
    InputStream inputStream = fileItem.getInputStream();
    FileOutputStream fileOutputStream = new FileOutputStream("C:\Users\huyoufu\Desktop\upload\"+name);
    //使用ioutils
    IOUtils.copy(inputStream, fileOutputStream);
    fileOutputStream.close();
    inputStream.close();
    }
    }
    } catch (FileUploadException e) {
    e.printStackTrace();
    }
    //4.返回list集合 装有解析好的 fileItem

  • 相关阅读:
    软件开发记录04
    《敏捷软件需求》阅读笔记02
    软件开发记录03
    《敏捷软件需求》阅读笔记01
    软件开发记录02
    Leetcode
    Leetcode
    Leetcode
    leetcode -625-Minimum Factorization
    51Nod
  • 原文地址:https://www.cnblogs.com/shan1393/p/9241210.html
Copyright © 2011-2022 走看看