package controller;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.Part;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.multipart.MultipartFile;
//@RequestPart 是根据 'Content-Type' 的
//@RequestParam是根据 Converter or PropertyEditor的
@Controller
public class FileUploadController
{
@RequestMapping(value="/fileUpload", method=RequestMethod.POST)
public String handleFormUpload(@RequestParam("name") String name,MultipartFile file) throws IOException
{
if(!file.isEmpty())
{
String filename = file.getOriginalFilename();
System.out.println(filename);
byte[] bytes = file.getBytes();
return "uploadSuccess";
}
else
return "uploadFailed";
}
/*
* The main difference is that when the method argument is not a String,
* @RequestParam relies on type conversion via a registered Converter or PropertyEditor
* while @RequestPart relies on HttpMessageConverters
* taking into consideration the 'Content-Type' header of
* the request part. @RequestParam is likely to be used with name-value
* form fields while @RequestPart is likely to be used with parts containing
* more complex content (e.g. JSON, XML).
*/
@RequestMapping(value="/fileUpload2", method=RequestMethod.POST)
public String handleFormUpload2(HttpServletRequest req,@RequestParam("name") String name,
@RequestPart Part file) throws IOException
{
if(file.getSize() != 0)
{
/*
* 取得"/"表示的tomcat中的实际路径
* http://<host>:<port>/<contextPath>/index.html would be mapped,
* where <contextPath> corresponds to the context path of this ServletContext.
*/
String uploadPath = req.getServletContext().getRealPath("/");
System.out.println("------ upLoadPath:" + uploadPath + " ------");
//从header中解译出上传的文件名
String value = file.getHeader("content-disposition");
System.out.println("------ value:" + value + " ------");
String filename = value.substring(value.lastIndexOf("=") + 2, value.length() - 1);
System.out.println("------ filename:" + filename + " ------");
//end
System.out.println("------ the file size is:" + file.getSize() + "B ------");
System.out.println("------ " + file.getName() + " ------");
//将文件写到硬盘上
file.write(uploadPath + filename);
return "uploadSuccess";
}
else
return "uploadFailed";
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
SpringMVC_RequestPart_文件上传_example.rar
共22个文件
jsp:4个
xml:3个
properties:2个
4星 · 超过85%的资源 需积分: 33 58 下载量 50 浏览量
2014-07-31
00:36:10
上传
评论
收藏 14KB RAR 举报
温馨提示
SpringMVC_RequestPart_文件上传_example.rar
资源推荐
资源详情
资源评论
收起资源包目录
20_RequestPart_文件上传.rar (22个子文件)
20_RequestPart_文件上传
.project 2KB
.mymetadata 329B
src
controller
FileUploadController.java 3KB
log4j.properties 2KB
WebRoot
uploadFailed.jsp 851B
WEB-INF
classes
controller
FileUploadController.class 3KB
log4j.properties 2KB
lib
web.xml 2KB
dispatcher-servlet.xml 2KB
fileUpload.jsp 1KB
uploadSuccess.jsp 837B
index.jsp 829B
META-INF
MANIFEST.MF 36B
.myeclipse
.settings
org.eclipse.wst.jsdt.ui.superType.container 49B
org.eclipse.wst.common.project.facet.core.xml 252B
org.eclipse.jdt.core.prefs 395B
org.eclipse.wst.jsdt.ui.superType.name 6B
com.genuitec.eclipse.j2eedt.core.prefs 1KB
org.eclipse.wst.common.component 454B
.jsdtscope 500B
.classpath 606B
SpringMVC文件上传_基于Servlet3.0 300B
共 22 条
- 1
资源评论
- relative6602016-09-01挺好的例子,有参考价值
- awszghj2018-02-28挺好的例子,具备参考价值。
Arshinz
- 粉丝: 1
- 资源: 6
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功