• 文件上传下载——servlet


    fileupload 组件可以将所有的请求信息都解析成 FileIteam 对象,可以通过对 FileItem 对象的操作完成 上传,⾯向对象的思想。

    复制代码
    package com.southwind.servlet;
    import org.apache.commons.fileupload.FileItem;
    import org.apache.commons.fileupload.FileUploadException;
    import org.apache.commons.fileupload.disk.DiskFileItemFactory;
    import org.apache.commons.fileupload.servlet.ServletFileUpload;
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.*;
    import java.util.List;
    @WebServlet("/upload")
    public class UploadServlet extends HttpServlet {
     @Override
     protected void doGet(HttpServletRequest req, HttpServletResponse resp)
    throws ServletException, IOException {
     }
     @Override
     protected void doPost(HttpServletRequest req, HttpServletResponse resp)
    throws ServletException, IOException {
    // //通过输⼊流获取客户端传来的数据流
    // InputStream inputStream = req.getInputStream();
    // Reader reader = new InputStreamReader(inputStream);
    // BufferedReader bufferedReader = new BufferedReader(reader);
    // //通过输出流将数据流输出到本地硬盘
    // //获取⽂件夹的绝对路径
    // String path = req.getServletContext().getRealPath("file/copy.txt");
    // OutputStream outputStream = new FileOutputStream(path);
    // Writer writer = new OutputStreamWriter(outputStream);
    // BufferedWriter bufferedWriter = new BufferedWriter(writer);
    // String str = "";
    // while((str = bufferedReader.readLine())!=null){
    // System.out.println(str);
    // bufferedWriter.write(str);
    // }
    // bufferedWriter.close();
    // writer.close();
    // outputStream.close();
    // bufferedReader.close();
    // reader.close();
    // inputStream.close();
     try {
     DiskFileItemFactory fileItemFactory = new DiskFileItemFactory();
     ServletFileUpload servletFileUpload = new
    ServletFileUpload(fileItemFactory);
     List<FileItem> list = servletFileUpload.parseRequest(req);
     for(FileItem fileItem : list){
     if(fileItem.isFormField()){
     String name = fileItem.getFieldName();
     String value = fileItem.getString("UTF-8");
     System.out.println(name+":"+value);
     }else{
     String fileName = fileItem.getName();
     long size = fileItem.getSize();
     System.out.println(fileName+":"+size+"Byte");
     InputStream inputStream = fileItem.getInputStream();
    // Reader reader = new InputStreamReader(inputStream);
    // BufferedReader bufferedReader = new
    BufferedReader(reader);
     String path =
    req.getServletContext().getRealPath("file/"+fileName);
     OutputStream outputStream = new FileOutputStream(path);
    // Writer writer = new OutputStreamWriter(outputStream);
    // BufferedWriter bufferedWriter = new
    BufferedWriter(writer);
     int temp = 0;
     while((temp = inputStream.read())!=-1){
     outputStream.write(temp);
     }
    // bufferedWriter.close();
    // writer.close();
     outputStream.close();
    // bufferedReader.close();
    // reader.close();
     inputStream.close();
     System.out.println("上传成功");
     }
     }
     } catch (FileUploadException e) {
     e.printStackTrace();
     }
     }
    }
  • 相关阅读:
    javaweb学习(十一)—— 使用Cookie进行会话管理
    微信小程序授权登录-获取用户信息-手机号信息-及解密碰到的坑
    mysql数据库乱码问题,数据库和程序链接过程中查询和显示出现中文乱码
    javaWeb项目启动加载java类执行自己想要执行的操作
    spring mvc 把事物处理配置到Controller层
    maven项目外网向内网搬迁问题之找不到本地仓库已经有的jar包
    java 解析json 的各种jar包
    jquery ui 插架 之 dataTable 显示横向滚动条
    jsp spring Mvc前台提交数据到后天乱码
    java cs tab点击切换标签的实现 panel
  • 原文地址:https://www.cnblogs.com/lmygq1728/p/14160890.html
Copyright © 2020-2023  润新知