springMVC框架本地服务器
1.页面:
<form action="fileUpload2" method="post" enctype="multipart/form-data">
用户名:<input type="text" name="username"> <br>
文件: <input type="file" name="imgFile"> <br>
<input type="submit" value="上传">
</form>
2.控制器
package com.haifeng.file_upload; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletRequest; import java.io.File; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.UUID; /** * springMVC文件上传 */ @Controller public class FileUploadController2 { //springMVC框架为我们提供了一个对象MultipartFile,该对象可以作为控制器方法的参数 //参数的名称和表单file元素的name属性必须一致 @RequestMapping("fileUpload2") public String testFileUpload(String name, HttpServletRequest request, MultipartFile imgFile) throws IOException { System.out.println("username=" + name); //1.设置上传文件目录(发布路径) String basePath = request.getSession().getServletContext().getRealPath("/upload"); //当前时间作为二级目录 String date = new SimpleDateFormat("yyyy-MM-dd").format(new Date()); //3 创建文件file对象,判断文件路径是否存在 File file = new File(basePath,date); if (!file.exists()) { file.mkdirs(); } //4 获取文件名 String fileName = imgFile.getOriginalFilename(); //5 为防止文件重名,最终文件名= uuid+fileName String uuidFileName = UUID.randomUUID().toString().replaceAll("_","").toUpperCase() + "_" + fileName; //6 上传文件 imgFile.transferTo(new File(file,uuidFileName)); return "success"; } } |
springMVC.xml配置
<?xml version="1.0" encoding="UTF-8"?> 注意: |
springMVC框架其他服务器
1.文件服务器的部署
(1) 先解压一个tomcat服务器,这里用的是tomcat9
(什么版本都没有关系:如tomcat6、tomcat7、tomcat8、tomcat9)
(2) 修改web.xml配置文件
如:
修改内容:
默认 readonly 为 true,当 readonly 设置为 false 时,可以通过一些方式进行文件操控。
(3) 创建一个文件系统,保存所有文件(文件上传到这里)
在tomcat的webapps目录下新建fileSystem文件系统
在fileSystem中新建upload目录、indexjsp测试文件即可
在conf/server.xml中修改tomcat端口
tomcat服务端口默认8080,修改为:6080
启动tomcat,访问index.jsp
这里只是测试下,
2导入 jersey 的坐标
<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>
3.编写控制器实现上传图片
-
@Controller
public class FileUploadController3 {
// 文件服务器地址
private static final String FILESERVERURL = "http://localhost:6080/fileSystem/upload/";
/**
* springmvc框架为我们提供了一个对象MultipartFile,该对象可以作为控制器方法的参数。
*/
@RequestMapping("fileUpload3")
public String testFileUpload(MultipartFile imgFile) throws Exception {
//1. 获取文件名
String fileName = imgFile.getOriginalFilename();
//2. 为了防止文件名重名,最终文件名=uuid + "_" + fileName
String uuidFileName =
UUID.randomUUID().toString().replaceAll("-", "").toUpperCase() + "_" + fileName;
//3.创建jersey包中提供的client对象
Client client = new Client();
//4.使用client和文件服务器建立联系
WebResource resource = client.resource(FILESERVERURL + uuidFileName);
//5.把web资源对象写到文件服务器
resource.put(String.class,imgFile.getBytes());
return "success";
}
}
4.bean.xml配置文件上传解析器
<!--文件上传解析器-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!--最大10M-->
<property name="maxUploadSize" value="10485760"></property>
</bean>
5.页面
<form action="fileUpload3" method="post" enctype="multipart/form-data">
用户名:<input type="text" name="username"> <br>
文件: <input type="file" name="imgFile"> <br>
<input type="submit" value="上传">
</form>