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

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

1 通过commons-fileupload来实现 导入相关jar包

commons-fileupload,commons-io

2 配置springmvc的配置解析器

mvc:

 

3 jsp页面

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%>              My JSP 'index.jsp' starting page    
file:

4 controller代码

@Controllerpublic class FileUploadController {    @RequestMapping("/upload")    public String fileupload(@RequestParam("file")CommonsMultipartFile file,HttpServletRequest req) throws IOException{        //获取文件名        //file.getOriginalFilename();        //获取上传文件的路径        String path = req.getRealPath("/fileupload");        InputStream is = file.getInputStream();        OutputStream os = new FileOutputStream(new File(path,file.getOriginalFilename()));        int len = 0;        byte[] buffer = new byte[400];        while((len=is.read(buffer))!=-1){            os.write(buffer,0,len);            os.close();            is.close();        }                    return "/index.jsp";    }}

批量上传的代码

@RequestMapping("/batch")    public String fileupload(@RequestParam("file")CommonsMultipartFile file[],            HttpServletRequest req) throws IOException{        //获取文件名        //file.getOriginalFilename();        //获取上传文件的路径        String path = req.getRealPath("/fileupload");        for (int i = 0; i < file.length; i++) {                    InputStream is = file[i].getInputStream();        OutputStream os = new FileOutputStream(new File(path,file[i].getOriginalFilename()));        int len = 0;        byte[] buffer = new byte[400];        while((len=is.read(buffer))!=-1)            os.write(buffer,0,len);            os.close();            is.close();                }                    return "/index.jsp";    }

 

转载于:https://www.cnblogs.com/alloevil/p/6072035.html

你可能感兴趣的文章
hdu 6435 CSGO(最大曼哈顿距离)
查看>>
logback框架之——日志分割所带来的潜在问题
查看>>
链路追踪工具之Zipkin学习小记
查看>>
iOS中通讯录的开发
查看>>
怎么让table中的<td>内容向上对齐
查看>>
[Java] 遍历HashMap和HashMap转换成List的两种方式
查看>>
mongodb
查看>>
LeetCode 46. Permutations
查看>>
jmeter- 性能测试3:聚合报告(Aggregate Report )
查看>>
JavaScript高级程序设计---学习笔记(二)
查看>>
vim 插件的学习
查看>>
Uncaught SyntaxError: Unexpected token ILLEGAL
查看>>
一个预处理定义的问题
查看>>
神经网路-SGD-1
查看>>
安卓冷知识:LayoutParams
查看>>
JAVA操作mysql(如何更加面向对象的操作数据库)
查看>>
Python9-事件及队列-day37
查看>>
ANDROID L——Material Design综合应用(Demo)
查看>>
django使用户名和邮箱都能登录
查看>>
Java是否可用public成员变量?
查看>>