• 文件上传下载——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();
     }
     }
    }
  • 相关阅读:
    Java中使用Log4j记录错误、输出日志
    oracle 触发器的实例(转)
    [jsp学习笔记]servelt get post
    [winfrom]C#中使用SendMessage
    3)创建,测试,发布 第一个NET CORE程序
    DDL和DML 的区别
    [jsp学习笔记] jsp过滤器
    [jsp学习笔记] jsp基础知识 数据初始化、同步
    [jsp学习笔记]jstl标签的使用
    LiteORM-For-DotNet,我的第一个开源库……更新
  • 原文地址:https://www.cnblogs.com/Gazikel/p/14139397.html
Copyright © 2020-2023  润新知