文件上传fileupload实现

2023年5月23日13:09:43

添加Pom.xml依赖:

<!--上传-->

<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload -->
<dependency>
    <groupId>commons-fileupload</groupId>
    <artifactId>commons-fileupload</artifactId>
    <version>1.3.1</version>
</dependency>

springmvc里面配置文件:

<bean id="multipartResolver"
      class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <property name="defaultEncoding" value="UTF-8" />
    <!-- 指定所上传文件的总大小,单位字节。注意maxUploadSize属性的限制不是针对单个文件,而是所有文件的容量之和 -->
    <property name="maxUploadSize" value="10240000" />
</bean>

jsp页面:

<form action="StockController/addStock.action"  method="post" enctype="multipart/form-data">
	<input type="file" name="multipartFile"/><br/>
	<input type="submit" value="添加">
</form>

Controller页面:

@RequestMapping(value = "addStock" ,method = RequestMethod.POST)
    public String addStock(Stock stock, MultipartFile multipartFile, HttpServletRequest request) throws IOException {
        
     
        System.out.println(multipartFile + "-------------------------------");
//获取文件名字
        String originalFilename = multipartFile.getOriginalFilename();
        System.out.println(originalFilename + "++++++++++++++++++++");
//获取不会重复的毫秒数
        long l = System.currentTimeMillis();
//新名字
        String newName = l + originalFilename;
        System.out.println(newName + "----------++++++");
//图片的输入流名字
        InputStream inputStream = multipartFile.getInputStream();
//存在于项目中的路径
        String wlPath = "E:\\小实训1\\xupeng20170919\\web\\image/" + newName;
//逻辑路径,存到数据库中
        String ljPath = "../image/" + newName;
//临时路径
        String realPath = request.getSession().getServletContext().getRealPath("/");
        String lsPath = realPath + "//image//" + newName;

//如果没有那个文件夹就创建
        File wlFile = new File("E:\\小实训1\\xupeng20170919\\web\\image");
        if (!wlFile.exists()) {
            wlFile.mkdir();
        }
        File isFile = new File(realPath + "\\image\\");
        if (!isFile.exists()) {
            isFile.mkdir();
        }
//不为空时
        if (!multipartFile.isEmpty()) {
            //输出流 写物理路径
            FileOutputStream wlStream = new FileOutputStream(wlPath);
            //输出流 写临时路径
            FileOutputStream lsStream = new FileOutputStream(lsPath);
            int len = 0;
            while ((len = inputStream.read()) != -1) {
                wlStream.write(len);//写入
                lsStream.write(len);

            }
            wlStream.flush();
            lsStream.flush();

            wlStream.close();
            lsStream.close();

            inputStream.close();

        }
        System.out.println(wlPath);
        System.out.println(ljPath);
        System.out.println(lsPath);
stock.setStockPhoto(ljPath);
        stockService.addStock(stock);
        
        return  "redirect:findStockAll.action";
        
    }

  • 作者:卢本伟
  • 原文链接:https://blog.csdn.net/qq_39514287/article/details/78049044
    更新时间:2023年5月23日13:09:43 ,共 2368 字。