博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SpringMvc文件上传
阅读量:6277 次
发布时间:2019-06-22

本文共 5740 字,大约阅读时间需要 19 分钟。

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            Iterator
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}"; }}

 

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}";}}

 

转载于:https://www.cnblogs.com/fangqiangblog/p/7847303.html

你可能感兴趣的文章
树莓派3链接wifi
查看>>
js面向对象编程
查看>>
Ruby中类 模块 单例方法 总结
查看>>
jQuery的validate插件
查看>>
5-4 8 管道符 作业控制 shell变量 环境变量配置
查看>>
Enumberable
查看>>
开发者论坛一周精粹(第五十四期) 求购备案服务号1枚!
查看>>
validate表单验证及自定义方法
查看>>
javascript 中出现missing ) after argument list的错误
查看>>
使用Swagger2构建强大的RESTful API文档(2)(二十三)
查看>>
Docker容器启动报WARNING: IPv4 forwarding is disabled. Networking will not work
查看>>
(转)第三方支付参与者
查看>>
程序员修炼之道读后感2
查看>>
DWR实现服务器向客户端推送消息
查看>>
js中forEach的用法
查看>>
Docker之功能汇总
查看>>
!!a标签和button按钮只允许点击一次,防止重复提交
查看>>
(轉貼) Eclipse + CDT + MinGW 安裝方法 (C/C++) (gcc) (g++) (OS) (Windows)
查看>>
还原数据库
查看>>
作业调度框架 Quartz.NET 2.0 beta 发布
查看>>