单个文件上传+多个文件上传

单个文件上传

  1. jsp页面
   <body>
   <form action="shangchuan.do"  method="post" enctype="multipart/form-data">
        文件<input type="file" name="fileName"> //name与方法参数保持一致
       <input type="submit" value="上传">
    </form>
  </body>`

2.文件上传的视图解析器

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
</bean>

3.编写Controller

package com.controller;

import java.io.File;
import java.io.IOException;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

@Controller
public class uploadController {
    @RequestMapping("shangchuan.do")
    public String upload(@RequestParam(value="fileName")MultipartFile fileName,HttpServletRequest request) throws IllegalStateException, IOException{
       //获取上传文件的 原始名称:123+时间戳.jpg
        String fname=fileName.getOriginalFilename();
               fname=System.currentTimeMillis()+fname;//时间戳
        System.out.println("原始名:"+fname);
        System.out.println("临时名:"+fileName.getName());

        //获取服务器上的upload文件夹的物理位置  :e:\tomcat\...\...
        String path=request.getSession().getServletContext().getRealPath("/upload");
        System.out.println("upload文件夹位置:"+path);

        File target=new File(path+"\\"+fname);
         fileName.transferTo(target);保存文件到指定目录
        return "success.jsp";
    }

4.文件夹:服务器的文件夹
上传后的信息最终会放到服务器上的upload文件夹中
这里写图片描述
5.结果:
这里写图片描述
这里写图片描述
这里写图片描述

多个文件上传

  1. jsp页面
   <body>
   <form action="duoshangchuan.do"  method="post" enctype="multipart/form-data">
        <input type="file" name="files"> <br>
        <input type="file" name="files"> <br>
        <input type="file" name="files"> <br>
       <input type="submit" value="都上传">
    </form>
  </body>`

2.文件上传的视图解析器

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
</bean>

3.编写Controller

package com.controller;

import java.io.File;
import java.io.IOException;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

@Controller
public class uploadController {
    @RequestMapping("duoshangchuan.do")
    public String duoshangchuan(@RequestParam(value="files")MultipartFile[] files,HttpServletRequest request) throws IllegalStateException, IOException{

        if(files!=null&&files.length>0){
          for(int i=0;i<files.length;i++){
              MultipartFile file=files[i];
              String fname=file.getOriginalFilename();
                     fname=System.currentTimeMillis()+fname;
              String path=request.getSession().getServletContext().getRealPath("/upload");
              System.out.println("路径:"+path+"名:"+fname);
              File target=new File(path+"/"+fname);
               file.transferTo(target);
          }
        }
        return "success.jsp";
    }
}

4.文件夹:服务器的文件夹
上传后的信息最终会放到服务器上的upload文件夹中
这里写图片描述
5.结果:
这里写图片描述
这里写图片描述
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值