springMvc 系列 6-文件上传
普通文件上传
在index.jsp页面上放置上传按钮
<h3>传统文件上传</h3>
<a href="demo">demo</a>
<form action="fileUpload1" method="post" enctype="multipart/form-data">
选择文件:<input type="file" name="upload" /><br/>
<input type="submit" value="上传" />
</form>
导入文件上传的坐标
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.3</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
添加控制器方法来处理并生成文件,该文件放置于tomcat文件夹里
@RequestMapping("/fileUpload1")
public String fileUpload(HttpServletRequest request) throws Exception{
//先获取到要上传的文件目录
String path = request.getSession().getServletContext().getRealPath("/uploads");
//创建File对象
File file = new File(path);
if(!file.exists()){
file.mkdirs();
}
//创建磁盘文件项工厂
DiskFileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload fileUpload = new ServletFileUpload(factory);
//解析request对象
List<FileItem> list = fileUpload.parseRequest(request);
//遍历
for (FileItem fileItem : list) {
//判断文件项是普通字段,还是上传的文件
if(fileItem.isFormField()){
//普通表单
}else{
//上传文件
String filename = fileItem.getName();
//把文件的名称设置唯一值,uuid
String uuid = UUID.randomUUID().toString().replace("-","");
filename=uuid+"_"+filename;
fileItem.write(new File(path, filename));//上传文件
fileItem.delete();//删除临时文件
}
}
return "success";
}
springMVC上传
springMVC框架提供了MultipartFile对象,该对象表示上传的文件,要求变量名称必须和表单file标签的name属性名称相同。
<!--配置文件解析器对象-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="10485760" />
</bean>
在index.jsp页面上放置上传按钮
<h3>Springmvc文件上传</h3>
<form action="fileUpload2" method="post" enctype="multipart/form-data">
选择文件:<input type="file" name="upload" /><br/>
<input type="submit" value="上传" />
</form>
添加控制器方法来处理并生成文件
@RequestMapping("/fileUpload2")
public String fileUpload2(HttpServletRequest request, MultipartFile upload) throws Exception{
System.out.println("springmvc文件上传...");
//使用fileupload组件完成文件上传
String path = request.getSession().getServletContext().getRealPath("/uploads");
File file = new File(path);
if(!file.exists()){
file.mkdirs();
}
//获取上传文件的名称
String filename = upload.getOriginalFilename();
//把文件的名称设置唯一值,uuid
String uuid = UUID.randomUUID().toString().replace("-","");
filename = uuid + "_"+filename;
//完成文件上传
upload.transferTo(new File(path,filename));
return "success";
}
跨服务器文件上传
在index.jsp页面上放置上传按钮
<h3>跨服务器文件上传</h3>
<form action="fileUpload3" method="post" enctype="multipart/form-data">
选择文件:<input type="file" name="upload" /><br/>
<input type="submit" value="上传" />
</form>
使用springMVC上传时需要添加坐标
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-core</artifactId>
<version>1.18.1</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-client</artifactId>
<version>1.18.1</version>
</dependency>
添加控制器方法来处理并生成文件
@RequestMapping("fileUpload3")
public String fileUpload3(MultipartFile upload) throws Exception{
//定义上传文件服务器路径
String path = "http://localhost:9090/uploads";
String filename = upload.getOriginalFilename();
String uuid = UUID.randomUUID().toString().replace("-","");
filename = uuid + "_" + filename;
//创建客户端的对象
Client client = Client.create();
//和图片服务器进行连接
WebResource webResource = client.resource(path + filename);
//上传文件
webResource.put(upload.getBytes());
return "success";
}