当前位置:   article > 正文

tomcat10上传文件两种方式,两种亲测有效_tomcat文件上传

tomcat文件上传

前言

tomcat10将javax.* 转换为 jakarta.*。这样就导致上传文件时引用的jar包有点区别。

第一种:fileupload控件上传

目前为止commons-fileupload最新的1.5也只支持 javax.servlet.http.HttpServletRequest
反编译工具看到的jar包信息,贴出来先看看

引入jar包
<dependency>
    <groupId>commons-fileupload</groupId>
    <artifactId>commons-fileupload</artifactId>
    <version>1.4</version>
</dependency>
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>4.0.1</version>
    <scope>provided</scope>
</dependency>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
jsp页面
<form action="updFile" method="post" enctype="multipart/form-data">
     file:<input type="file" name="file" /><p>
     title:<input type="text" name="title" /><p>
     <input type="submit" value="提交" /><p>
</form>
  • 1
  • 2
  • 3
  • 4
  • 5
controller
  class Rc implements RequestContext{        
      HttpServletRequest request = null;
      public Rc(HttpServletRequest request) {
          this.request=request;
      }

      @Override
      public String getCharacterEncoding() {
          return request.getCharacterEncoding();
      }

      @Override
      public String getContentType() {
          return request.getContentType();
      }

      @Override
      public int getContentLength() {
          return request.getContentLength();
      }

      @Override
      public InputStream getInputStream() throws IOException {
          return request.getInputStream();
      }
  }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

new Rc(request)是tomcat10上传文件时的重点,jakarta.* ——> javax.*

	FileItemFactory factory = new DiskFileItemFactory();
    ServletFileUpload upload = new ServletFileUpload(factory);
    //new Rc(request)
    //jakarta.*->javax.*
    List<FileItem> items = upload.parseRequest(new Rc(request));
	try {
	    Iterator<FileItem> iter = items.iterator();
	    while (iter.hasNext()) {
		     FileItem item = iter.next();
		     //非文件类型
		     if (item.isFormField()) {
		      String itemName = item.getFieldName();
		      if (itemName.equals("selectedBusho")) {//selectedBusho-》jsp name属性
			       String selectedBusho = null;
			       selectedBusho = item.getString("utf-8"); //获取字段上的值,下拉框获取的是key
			       photoBean.setSelectedBusho(selectedBusho);
		      }
		      else if (itemName.equals("title")) {
			      String title = null; 
			      title = item.getString("utf-8");
			      photoBean.setTitle(title);
		     }
		     //文件类型
		    } else {
		      String fileName = item.getName();
		      if("".equals(fileName)){
		    	  fileName=dephotoBean.getDspFileName();
		      }
		      photoBean.setFileName(fileName);
		    }
		   item.delete();
	    }
	}catch (Exception e) {
	    e.printStackTrace();
	}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35

第二种:MultipartFile文件上传

引入jar包
<dependency>
    <groupId>commons-fileupload</groupId>
    <artifactId>commons-fileupload</artifactId>
    <version>1.4</version>
</dependency>
  • 1
  • 2
  • 3
  • 4
  • 5
springmvc.xml

这个id不能换

     <bean id="multipartResolver" class="org.springframework.web.multipart.support.StandardServletMultipartResolver">   
	</bean>
  • 1
  • 2
web.xml

multipart-config 标签里面有很多参数,自行追加

<servlet>
		<servlet-name>springDispatcherServlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:springmvc.xml</param-value>
		</init-param>
		<load-on-startup>2</load-on-startup>
		<multipart-config>
			<file-size-threshold>0</file-size-threshold>
		</multipart-config>
</servlet>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
jsp
<form action="updFile" method="post" enctype="multipart/form-data">
     file:<input type="file" name="file" /><p>
     title:<input type="text" name="title" /><p>
     <input type="submit" value="提交" /><p>
</form>
  • 1
  • 2
  • 3
  • 4
  • 5
controller
@Controller
@MultipartConfig
public class UploadHandler  extends DEEHandlerBase {

	@RequestMapping("/updFile")
    public ModelAndView UploadTheme(@RequestPart("file") MultipartFile meFile,
    	HttpServletRequest request) throws IllegalStateException, IOException{
    	
		HttpSession session=request.getSession();
		DEEPhotoBean photoBean = new DEEPhotoBean();
		photoBean.setSelectedBusho(request.getParameter("selectedBusho"));
		photoBean.setTitle(request.getParameter("title"));//name属性等于title的值
		if (meFile != null) {	
			meFile.transferTo(new File("E:/temp/a.jsp"));
			photoBean.setFileName(meFile.getOriginalFilename());//文件名
		}
        session.setAttribute("fileUploadPhotoBean", photoBean);
        mv.setViewName("redirect:/doPhotoCheck");
        return mv;
	}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
@Controller
public class DEEPhotoHandler{
@RequestMapping("/doPhotoCheck") 
public ModelAndView doCheck(
	final HttpServletRequest request,
    final DEEPhotoBean photoBean
  ){
	  HttpSession session = request.getSession();
	  DEEPhotoBean fileUploadhotoBean= (DEEPhotoBean) session.getAttribute("fileUploadPhotoBean");
	  photoBean.setSelectedBusho(fileUploadhotoBean.getSelectedBusho());
	  photoBean.setTitle(fileUploadhotoBean.getTitle());
	  session.setAttribute("photoBean", photoBean);
	  mv.setViewName("SetPhoto");
	  return mv;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/运维做开发/article/detail/756041
推荐阅读
相关标签
  

闽ICP备14008679号