06springmvc文件上传.txt springmvc 4.3.9 还支持servlet3 文件上传------------------------------------------------------------ 单文件上传 作者: 文件: @WebServlet("/upload.do") @MultipartConfigpublic class Upload extends
06springmvc文件上传.txt
springmvc 4.3.9 还支持servlet3 文件上传
------------------------------------------------------------
单文件上传
@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 文件加入依赖
commons-fileupload
commons-fileupload
1.3.2
会自动引入如下两个包
commons-fileupload.jar
commons-io.jar
2、打开src/main/resources/mvc.xml springmvc核心配置文件
3、表单文件
单文件上传
4、controller控制器 up.action 编写如下
控制器方法参数注解如下
此处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 多文件上传及表单内容接收
commons-fileupload
commons-fileupload
1.3.2
会自动引入如下两个包
commons-fileupload.jar
commons-io.jar
src/main/resources/mvc.xml springmvc 框架核心配置文件加入bean配置
SpringMVC 多文件上传
//@RequestMapping("/ups.action") @ResponseBody
//public void uploads(@RequestParam("face") CommonsMultipartFile fs[],HttpServletRequest req) throws IOException {
@RequestMapping("/ups.action")
public ModelAndView uploads(@RequestParam("face") List
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
info = new ArrayList
(); 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
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
info = new ArrayList
(); 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); //接表单值 req.setAttribute("author",xxx); m.addObject("author",req.getParameter("author")); return m; } 如何解决接到的数据中文乱码 //判断该文件是否为普通表单 if (item.isFormField()) { String name = item.getFieldName(); String value = null; try { value = item.getString("utf8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } //获取表单内容 if (name.equalsIgnoreCase("teacherName")) { ClassroomUploadVo.setTeacherName(value); }