赞
踩
tomcat10将javax.* 转换为 jakarta.*。这样就导致上传文件时引用的jar包有点区别。
目前为止commons-fileupload最新的1.5也只支持 javax.servlet.http.HttpServletRequest
<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>
<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>
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(); } }
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(); }
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.4</version>
</dependency>
这个id不能换
<bean id="multipartResolver" class="org.springframework.web.multipart.support.StandardServletMultipartResolver">
</bean>
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>
<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>
@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; } }
@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;
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。