当前位置: 首页 > news >正文

Servlet3.0上传文件

页面:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>文件上传</title>
</head>
<body>
    <form action="fileup" enctype="multipart/form-data" method="post">
        <input type="file" name="file" required="required"/><br />
        <input name="remark" required="required"/><br/>
        <button type="submit">上传</button>
    </form>
</body>
</html>

后台:

/**
 * @MultipartConfig 3.0条加 注解MultipartConfig 就可以获取到参数值了
 */
@WebServlet("/fileup")
@MultipartConfig
public class FileUpLoadServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public FileUpLoadServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        request.setCharacterEncoding("UTF-8");
        
        String remark = request.getParameter("remark");
        System.out.println("获取到值remark:"+remark);
        
        //3.0可以 获取 文件
        Part file =  request.getPart("file");
        
        //保存文件到D盘
        String dest = "D:\\wwwroot";
        // 保存文件到一个上传的目录    file.getSubmittedFileName()
        System.out.println("file.getName()-->"+file.getName());
        System.out.println("file.getContentType()-->"+file.getContentType());
        System.out.println("file.getSize()-->"+file.getSize());
        System.out.println("file.getSubmittedFileName()-->"+file.getSubmittedFileName());
        System.out.println("file.hashCode()-->"+file.hashCode());
        System.out.println("file.toString()-->"+file.toString());
        System.out.println("file.getHeaderNames()-->"+file.getHeaderNames());
        //C:\Users\Administrator\Desktop\tup.ico  截取 最后一个 \  后面的文件名
        Path target = Paths.get(dest, file.getSubmittedFileName().substring(file.getSubmittedFileName().lastIndexOf(File.separator), file.getSubmittedFileName().length()));
        System.out.println("文件保存的目标路径:" + target);
        try (InputStream in = file.getInputStream()) {
            Files.copy(in, target);
        } finally {
            // Part对象使用完以后,需要删除掉,否则临时文件可能一直都在硬盘上
            file.delete();
        }
    }

}

http://www.lryc.cn/news/95209.html

相关文章:

  • 【ARM Cache 系列文章 6 番外篇 – MMU, MPU, SMMU, PMU 差异与关系】
  • NetSuite ERP顾问的进阶之路
  • js 新浏览器打开页面
  • jmeter软件测试实验(附源码以及配置)
  • ZooKeeper原理剖析
  • 【算组合数】CF1833 F
  • Attention详解(自用)
  • pptx转pdf工具类
  • 2023华为OD统一考试(B卷)题库清单(持续收录中)以及考点说明
  • 论文笔记--Won’t Get Fooled Again: Answering Questions with False Premises
  • 【Django】include app_name和namespace的区别
  • (黑客)自学笔记
  • 【期末课程设计】学生成绩管理系统
  • 【论文笔记】KDD2019 | KGAT: Knowledge Graph Attention Network for Recommendation
  • ES6:基础使用,积累
  • Android端上传文件到Spring Boot后端
  • 使用GGML和LangChain在CPU上运行量化的llama2
  • 微服务基础理论
  • 《向量数据库指南》:向量数据库Pinecone管理数据教程
  • 以深度为基础的Scikit-learn: 高级特性与最佳实践
  • Autosar MCAL-S32K324Dio配置-基于EB
  • 【Spring Boot】单元测试
  • Flink CEP (一)原理及概念
  • vue3+taro+Nutui 开发小程序(二)
  • Transformer 模型实用介绍:BERT
  • Spring详解(学习总结)
  • 【JavaEE】Spring中注解的方式去获取Bean对象
  • 【基于CentOS 7 的iscsi服务】
  • 解决安装依赖时报错:npm ERR! code ERESOLVE
  • 98、简述Kafka的rebalance机制