06springmvc文件上传
springmvc 4.3.9 还支持servlet3 文件上传
------------------------------------------------------------
<body>
<h3>单文件上传</h3>
<form action="upload.do" method="post" enctype="multipart/form-data">
作者:<input type="text" name="author"><br>
文件:<input type="file" name="ufile"><br>
<input type="submit" value="提交">
</form>
@WebServlet("/upload.do") @MultipartConfig
public class Upload extends HttpServlet{
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
String n = req.getParameter("author");
System.out.println(n);
Part pf = req.getPart("ufile");
String nnnn = pf.getSubmittedFileName();
System.out.println(nnnn);
String path = getServletContext().getRealPath("/upload/");
System.out.println(path);
File f = new File(path);
if(!f.exists()){
f.mkdirs();
}
pf.write(path+nnnn);
}
}
spring mvc 文件 上传
-----------------------------------------------------------------------
1、项目pom.xml 文件加入依赖
<!-- springmvc commons 文件上传 -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.2</version>
</dependency>
会自动引入如下两个包
commons-fileupload.jar
commons-io.jar
2、打开src/main/resources/mvc.xml springmvc核心配置文件
<!-- 文件上传支持 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 104857600 代表100MB -->
<property name="maxUploadSize" value="104857600" />
<property name="maxInMemorySize" value="40960" />
<property name="defaultEncoding" value="UTF-8"></property>
</bean>
3、表单文件
<h3>单文件上传</h3>
<form action="up.action" method="post" enctype="multipart/form-data">
作者:<input type="text" name="author"><br>
文件:<input type="file" name="ufile"><br>
<input type="submit" value="提交">
</form>
4、controller控制器 up.action 编写如下
控制器方法参数注解如下
此处ufile就是表单文件名称 <input type="file" name="ufile"/>
@RequestParam("ufile") CommonsMultipartFile f,
package com.fz.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
/**
* Created by webrx on 2017-06-28.
*/
@Controller
public class UploadFile {
@RequestMapping("/up.action")
public void upload(@RequestParam("ufile") CommonsMultipartFile f, HttpServletResponse resp, HttpServletRequest req) throws IOException {
String path = req.getServletContext().getRealPath("/upload/user/"); //项目根 /upload/user/ 此目录下
System.out.println(path);
File pa = new File(path);
if(!pa.exists()){
pa.mkdirs();
}
System.out.println(f.getBytes());
System.out.println(f.getContentType());
System.out.println(f.getName());//取得表单名称
System.out.println(f.getSize()); //文件大小
System.out.println(f.getOriginalFilename());//上传的原始文件名
}
}
5、springmvc 多文件上传及表单内容接收
<!-- springmvc commons 文件上传 -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.2</version>
</dependency>
会自动引入如下两个包
commons-fileupload.jar
commons-io.jar
src/main/resources/mvc.xml springmvc 框架核心配置文件加入bean配置
<!-- 文件上传支持 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 104857600 代表100MB -->
<property name="maxUploadSize" value="104857600" />
<property name="maxInMemorySize" value="40960" />
<property name="defaultEncoding" value="UTF-8"></property>
</bean>
<h3>SpringMVC 多文件上传</h3>
<form action="ups.action" method="post" enctype="multipart/form-data">
作者:<input type="text" name="author"><br>
文件:<input type="file" name="face" multiple><br>
<input type="submit" value="提交">
</form>
//@RequestMapping("/ups.action") @ResponseBody
//public void uploads(@RequestParam("face") CommonsMultipartFile fs[],HttpServletRequest req) throws IOException {
@RequestMapping("/ups.action")
public ModelAndView uploads(@RequestParam("face") List<CommonsMultipartFile> fs, HttpServletRequest req) throws IOException {
String path = req.getServletContext().getRealPath("/upload/booktxt/");
System.out.println(path);
File pa = new File(path);
if(!pa.exists()){
pa.mkdirs();
}
//System.out.println(fs.length);
System.out.println(fs.size());
ModelAndView m = new ModelAndView("ok");
List<String> info = new ArrayList<String>();
for(CommonsMultipartFile cmf : fs){
System.out.println(cmf.getOriginalFilename());
System.out.println(cmf.getSize());
cmf.transferTo(new File(path,cmf.getOriginalFilename()));
info.add(String.format("文件名:%s,大小写:%d字节",cmf.getOriginalFilename(),cmf.getSize()));
}
m.addObject("fs",info);
return m;
}
springmvc 多文件,单文件,上传时可以使用 req直接接表单字符串值
//@RequestMapping("/ups.action") @ResponseBody
//public void uploads(@RequestParam("face") CommonsMultipartFile fs[],HttpServletRequest req) throws IOException {
@RequestMapping("/ups.action")
public ModelAndView uploads(@RequestParam("face") List<CommonsMultipartFile> fs, HttpServletRequest req) throws IOException {
String path = req.getServletContext().getRealPath("/upload/booktxt/");
System.out.println(path);
File pa = new File(path);
if(!pa.exists()){
pa.mkdirs();
}
//System.out.println(fs.length);
System.out.println(fs.size());
ModelAndView m = new ModelAndView("ok");
List<String> info = new ArrayList<String>();
for(CommonsMultipartFile cmf : fs){
System.out.println(cmf.getOriginalFilename());
System.out.println(cmf.getSize());
cmf.transferTo(new File(path,cmf.getOriginalFilename()));
info.add(String.format("文件名:%s,大小写:%d字节",cmf.getOriginalFilename(),cmf.getSize()));
}
m.addObject("fs",info);
//接表单值 <input type="text" name="author" /> req.setAttribute("author",xxx);
m.addObject("author",req.getParameter("author"));
return m;
}