一. commons-fileupload组件上传
1. 导入jar包依赖
<!--文件上传组件-->
<!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
2. 编写文件上传的前台页面
<h3>文件上传</h3>
<form action="upload" method="post" enctype="multipart/form-data">
选择文件:<input type="file" name="upload"/><br/>
<input type="submit" value="上传文件"/>
</form>
3. 编写Controller控制器
/**
* 组件上传(commons-fileupload)
* @param request
* @return
* @throws Exception
*/
@PostMapping("upload")
@ResponseBody
public String uploadFile(HttpServletRequest request) throws Exception {
System.out.println("文件上传 start...");
//上传位置
String realPath = request.getSession().getServletContext().getRealPath("/uploads/");
//判断路径是否存在
File file = new File(realPath);
if(!file.isDirectory()){
//创建文件夹
boolean mkdir = file.mkdir();
System.out.println("位置:"+mkdir+":"+file.getAbsolutePath());
}
//解析request对象,获取上传文件项
//磁盘文件项工厂
DiskFileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
List<FileItem> items = upload.parseRequest(request);
for (FileItem i:items) {
if(i.isFormField()){
//普通表单项,不处理
}else{
//文件上传项
//获取上传文件的名称
String fileName = i.getName();
fileName = System.currentTimeMillis() + fileName;
System.out.println("文件名:"+fileName);
//上传
i.write(new File(realPath,fileName));
//删除临时文件
i.delete();
}
}
return "ok";
}
二. SpringMVC文件上传
1. SpringMVC框架提供了MultipartFile对象,该对象表示上传的文件,要求变量名称必须和表单file标签的 name属性名称相同。
2.Controller如下:
/**
* springMVC上传
* @param request
* @param upload
* @return
* @throws IOException
*/
@PostMapping("upload")
@ResponseBody
public String springMVCFileUpload(HttpServletRequest request,MultipartFile upload) throws IOException {
System.out.println("文件上传 start...");
//上传位置
String realPath = request.getSession().getServletContext().getRealPath("/uploads/");
//判断路径是否存在
File file = new File(realPath);
if(!file.isDirectory()){
//创建文件夹
boolean mkdir = file.mkdir();
System.out.println("创建文件夹"+mkdir+":"+file.getAbsolutePath());
}
//获取上传文件的名称
String fileName = upload.getOriginalFilename();
fileName = System.currentTimeMillis() + fileName;
System.out.println("文件名:"+fileName);
upload.transferTo(new File(realPath,fileName));
return "ok";
}
3.配置文件解析器对象
<!--配置文件上传解析器-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!--文件上传大小限制-->
<property name="maxUploadSize" value="10485760"/> <!-- 10M = 10*1024*1024 = 10485760 -->
</bean>
三. SpringMVC跨服务器文件上传
1.准备图片服务器(Server-image),并配置允许读写操作(以 http://localhost:9090/uploads/ (图片服务器的路径) 为例:)
<servlet>
<servlet-name>default</servlet-name>
<servlet-class>org.apache.catalina.servlets.DefaultServlet</servlet-class>
<!-- 添加,解决jersey上传服务器403错误 -->
<init-param>
<param-name>readonly</param-name>
<param-value>false</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>0</param-value>
</init-param>
<init-param>
<param-name>listings</param-name>
<param-value>false</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
2.编写控制类
/**
* 跨域上传
* @param upload
* @return
* @throws IOException
*/
@PostMapping("upload")
@ResponseBody
public String httpFileUpload(MultipartFile upload) throws IOException {
System.out.println("文件上传 start...");
// 上传位置
String realPath = "http://localhost:9090/uploads/";
String fileName = System.currentTimeMillis() + upload.getOriginalFilename();
// 创建客户端对象
Client client = Client.create();
// 连接图片服务器
WebResource resource = client.resource(realPath + fileName);
// 上传文件
resource.put(upload.getBytes());
return "ok";
}
3.上传测试结果