package cn.jingbang.controller;import java.io.BufferedOutputStream;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.util.Iterator;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.bind.annotation.ResponseBody;import org.springframework.web.multipart.MultipartFile;import org.springframework.web.multipart.MultipartHttpServletRequest;import org.springframework.web.multipart.commons.CommonsMultipartFile;import org.springframework.web.multipart.commons.CommonsMultipartResolver;/** * 为了实现文件上传的多种方式 * @author FangQiang * @Date 2017/11/16 */@Controllerpublic class UploadController { @RequestMapping("/up.do") public String up() { return "/upfile.jsp"; } @RequestMapping("/fileUpload1.do") @ResponseBody public String fileUpload(@RequestParam("file") CommonsMultipartFile file,HttpServletRequest request) throws IOException { String path = request.getSession().getServletContext().getRealPath("/")+"upload/"+file.getOriginalFilename(); System.out.println(path); //这里使用默认的缓冲输出流 OutputStream os = new BufferedOutputStream(new FileOutputStream(path)); // 这里给字节流加了缓冲 InputStream inputStream = file.getInputStream(); byte[] b = new byte[1024]; int i; while(inputStream.read(b) != -1) { os.write(b); } inputStream.close(); os.close(); //ajax返还json return "{\"success\":true}"; } @RequestMapping("fileUpload2") @ResponseBody public String fileUpload(@RequestParam("file") MultipartFile file,HttpServletRequest request) throws Exception, IOException { String path = request.getSession().getServletContext().getRealPath("/")+"upload/"+file.getOriginalFilename(); File newFile = new File(path); file.transferTo(newFile); return "{\"success\":true}"; } @RequestMapping("springUpload") @ResponseBody public String springUpload(HttpServletRequest request) throws IllegalStateException, IOException { CommonsMultipartResolver resolver = new CommonsMultipartResolver(request.getSession().getServletContext()); //如果是多部件上传 if(resolver.isMultipart(request)) { MultipartHttpServletRequest req = (MultipartHttpServletRequest) request; //得到所有的fileNames Iteratoriter = req.getFileNames(); while(iter.hasNext()) { MultipartFile file = req.getFile(iter.next().toString()); if(file != null) { String path = request.getSession().getServletContext() .getRealPath("/")+"upload/"+file.getOriginalFilename(); file.transferTo(new File(path)); } } } return "{\"success\":true}"; }}
package cn.jingbang.controller;
import java.io.BufferedOutputStream;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.util.Iterator;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.bind.annotation.ResponseBody;import org.springframework.web.multipart.MultipartFile;import org.springframework.web.multipart.MultipartHttpServletRequest;import org.springframework.web.multipart.commons.CommonsMultipartFile;import org.springframework.web.multipart.commons.CommonsMultipartResolver;/** * 为了实现文件上传的多种方式 * @author FangQiang * @Date 2017/11/16 */@Controllerpublic class UploadController {@RequestMapping("/up.do")public String up() {return "/upfile.jsp";}@RequestMapping("/fileUpload1.do")@ResponseBodypublic String fileUpload(@RequestParam("file") CommonsMultipartFile file,HttpServletRequest request) throws IOException {String path = request.getSession().getServletContext().getRealPath("/")+"upload/"+file.getOriginalFilename();System.out.println(path);//这里使用默认的缓冲输出流OutputStream os = new BufferedOutputStream(new FileOutputStream(path));// 这里给字节流加了缓冲InputStream inputStream = file.getInputStream();byte[] b = new byte[1024];int i;while(inputStream.read(b) != -1) {os.write(b);}inputStream.close();os.close();//ajax返还jsonreturn "{\"success\":true}";}@RequestMapping("fileUpload2")@ResponseBodypublic String fileUpload(@RequestParam("file") MultipartFile file,HttpServletRequest request) throws Exception, IOException {String path = request.getSession().getServletContext().getRealPath("/")+"upload/"+file.getOriginalFilename();File newFile = new File(path);file.transferTo(newFile);return "{\"success\":true}";}@RequestMapping("springUpload")@ResponseBodypublic String springUpload(HttpServletRequest request) throws IllegalStateException, IOException {CommonsMultipartResolver resolver = new CommonsMultipartResolver(request.getSession().getServletContext());//如果是多部件上传if(resolver.isMultipart(request)) {MultipartHttpServletRequest req = (MultipartHttpServletRequest) request;//得到所有的fileNamesIterator<String> iter = req.getFileNames();while(iter.hasNext()) {MultipartFile file = req.getFile(iter.next().toString());if(file != null) {String path = request.getSession().getServletContext().getRealPath("/")+"upload/"+file.getOriginalFilename(); file.transferTo(new File(path));}}}return "{\"success\":true}";}}