Spring mvc 文件上传到文件夹(转载+心得)
spring mvc(注解)上传文件的简单例子,这有几个需要注意的地方
1.form的enctype=”multipart/form-data” 这个是上传文件必须的
2.applicationContext.xml中 <bean id=”multipartResolver” class=”org.springframework.web.multipart.commons.CommonsMultipartResolver”/> 关于文件上传的配置不能少
applicationContext.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
- xmlns:context="http://www.springframework.org/schema/context"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"
- default-lazy-init="true">
- <!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->
- <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" lazy-init="false" />
- <!-- 另外最好还要加入DefaultAnnotationHandlerMapping,不然会被 XML或其它的映射覆盖! -->
- <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
- <!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 -->
- <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" />
- <!-- 支持上传文件 value表示上传文件的大小-->
- <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
- <property name="maxUploadSize" value="10000000000000"/></bean>
- </beans>
UploadAction.java
- package com.codeif.action;
- import java.io.File;
- import java.util.Date;
- import javax.servlet.http.HttpServletRequest;
- import org.springframework.stereotype.Controller;
- import org.springframework.ui.ModelMap;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestParam;
- import org.springframework.web.multipart.MultipartFile;
- @Controller
- public class UploadAction {
- @RequestMapping(value = "/upload.do")
- public String upload(@RequestParam(value = "file", required = false) MultipartFile file, HttpServletRequest request, ModelMap model) {
- System.out.println("开始");
- String path = request.getSession().getServletContext().getRealPath("upload");
- String fileName = file.getOriginalFilename();
- // String fileName = new Date().getTime()+".jpg";
- System.out.println(path);
- File targetFile = new File(path, fileName);
- if(!targetFile.exists()){
- targetFile.mkdirs();
- }
- //保存
- try {
- // file.transferTo(targetFile);
- //也可以用这种方式,上面的方式能上传,但是老是报输出流被占用的错误。
- SaveFileFromInputStream(
file.getInputStream(), realPath + filePath, name + "."+ fileType); - } catch (Exception e) {
- e.printStackTrace();
- }
- model.addAttribute("fileUrl", request.getContextPath()+"/upload/"+fileName);
- return "result";
- }
- }
SaveFileFromInputStream()的实现如下:
public void SaveFileFromInputStream(InputStream stream, String path,
String filename) throws IOException {
{ //path + "/"+ filename
FileOutputStream fs = new FileOutputStream(path + "/"+ filename);
byte[] buffer =new byte[1024*1024];
int bytesum = 0;
int byteread = 0;
while ((byteread=stream.read(buffer))!=-1)
{
bytesum+=byteread;
fs.write(buffer,0,byteread);
fs.flush();
}
fs.close();
stream.close();
}
index.jsp
- <%@ page pageEncoding="utf-8"%>
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8">
- <title>上传图片</title>
- </head>
- <body>
- <form action="upload.do" method="post" enctype="multipart/form-data">
- <input type="file" name="file" /> <input type="submit" value="Submit" /></form>
- </body>
- </html>
Spring mvc 文件上传到文件夹(转载+心得)的更多相关文章
- spring mvc 图片上传,图片压缩、跨域解决、 按天生成文件夹 ,删除,限制为图片代码等相关配置
spring mvc 图片上传,跨域解决 按天生成文件夹 ,删除,限制为图片代码,等相关配置 fs.root=data/ #fs.root=/home/dev/fs/ #fs.root=D:/fs/ ...
- spring mvc(注解)上传文件的简单例子
spring mvc(注解)上传文件的简单例子,这有几个需要注意的地方1.form的enctype=”multipart/form-data” 这个是上传文件必须的2.applicationConte ...
- SpringMVC单文件上传、多文件上传、文件列表显示、文件下载(转)
林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka 本文详细讲解了SpringMVC实例单文件上传.多文件上传.文件列表显示.文件下载. 本文工程 ...
- SpringMVC文件上传 Excle文件 Poi解析 验证 去重 并批量导入 MYSQL数据库
SpringMVC文件上传 Excle文件 Poi解析并批量导入 MYSQL数据库 /** * 业务需求说明: * 1 批量导入成员 并且 自主创建账号 * 2 校验数据格式 且 重复导入提示 已被 ...
- struts2文件上传,文件类型 allowedTypes
struts2文件上传,文件类型 allowedTypes 1 '.a' : 'application/octet-stream', 2 '.ai' : 'application/postscript ...
- webAPI文件上传时文件过大404错误的问题
背景:最近公司有个需求,外网希望自动保存数据到内网,内网有2台服务器可以相互访问,其中一台服务器外网可以访问,于是想在 这台服务器上放个中转的接口.后来做出来以后测试发现没有问题就放线上去了,不顾发现 ...
- spring mvc文件上传(单个文件上传|多个文件上传)
单个文件上传spring mvc 实现文件上传需要引入两个必须的jar包 1.所需jar包: commons-fileupload-1.3.1.jar ...
- java spring mvc restful 上传文件
spring mvc 配置文件 <bean class="com.baiyyy.yfz.core.RestfulHandlerMethodMapping" /> ...
- spring mvc CommonsMultipartResolver上传文件异常处理
近期已经上线的项目出现了一个异常 严重: Servlet.service() for servlet JeeCmsAdmin threw exception org.apache.commons.fi ...
随机推荐
- WebApi常见4xx错误总结!!!
iis 7上发布mvc报错:403.14-Forbidden Web 服务器被配置为不列出此目录的内容 折腾了半天,提示里面的解决方法是: 如果不希望启用目录浏览,请确保配置了默认文档并且该文件存在. ...
- dede自定义表单增加添加时间怎么弄
我们在用dedecms添加自定义表单时有时想要设置一个用户提交的时间,方便查询,比如我们的客服人员查询昨天晚上下班后有哪些订单是刚生成的,比较好查看,如下图所示.那么,dedecms自定义表单增加添加 ...
- 通过url地址传递base64加密参数遇到的问题整理
1. base64的加密解密方法在C#的类库中就有 QueryString中的加号变成了空格问题 Server.UrlEncode(username),获取到的编码又将等于号变成了%3d; 到底改怎么 ...
- maven之clean、install命令
1.进入到maven根目录,执行mvn compile命令会在根目录生成target文件(参照maven之helloworld案例),如下图: 2.执行mvn clean可将根目录下生成的target ...
- 比较两个数据库表table结构不同之处
/*--比较两个数据库的表字段差异 hy 适用多种版本库 --*/ /*--调用示例 exec p_comparestructure 'database1','database2' --*/ ) dr ...
- 01WebApi防篡改机制---HMAC机制
防篡改,顾名思义就是防止有人恶意篡改请求数据URL以达到恶意攻击的目的,那要怎么才能实现这样的目的呢? 很简单,将要请求的数据加上合作号.合作Key按规则组织成一个字符串,获取对应的MD5摘要,然后将 ...
- Fedora 25 Alpha版本今天发布啦
时隔Fedora 24发布后的3个月,Fedora项目团队非常开心的宣布任何感兴趣的用户都能下载和测试即将到来的Fedora 25操作系统的Alpha预发布版本,在Fedora 25 Alpha里程碑 ...
- java文档
http://www.boyunjian.com/javadoc/com.dyuproject.protostuff/protostuff-me/1.0.5/_/com/dyuproject/prot ...
- CCActionManager
当CCnode执行runAction的时候,runAction会调用动作管理类的addAction方法将它自己执行的动作传递给动作管理类,动作管理类再将动作添加到自己的动作序列中. 动过管理类通过定时 ...
- HTTP头部详解
因为之后的HTTP头注入要学习这些所以就看了.觉得很不错,算是学习前的科普. <HTTP头部详解>转载自:http://www.cnblogs.com/lcamry/p/5763040.h ...