03- 文件上传
参考视频:https://www.bilibili.com/video/BV1mE411X7yp?p=197
up主讲的很好,也欢迎去学习,(纯路人滴滴)
以下为纯手敲笔记~还有些不足,呜
- form 表单的enctyoe 取值必须是:multipart / form-data;(注意:默认:application / x-www-form-urlencoded ): enctype 是表单请求正文的类型;
- method 属性必须是 Post;
- 《input type=“ file ” 》;
**原理:**fileUpload:
request. getParameter ( )会失效;依赖坐标:
<dependency> <groupId>com.liferay.org.apache.commons.fileupload</groupId> <artifactId>com.liferay.org.apache.commons.fileupload</artifactId> <version>6.2.0.1</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.10.0</version> </dependency>
一般方法:
@RequestMapping("/fileUpload")
public String fileUpload(HttpServletRequest req){
System.out.println("文件上传成功。。。。。。");
String path = req.getSession().getServletContext().getRealPath("/uploads/");
File newFile = new File(path);
if(!newFile.exists()){
newFile.mkdirs();
}
// factory:
DiskFileItemFactory fileItemFactory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(fileItemFactory);//
// request :
try {
List<FileItem> lists = upload.parseRequest(req);
for (FileItem item : lists) {
if(item.isFormField()){
// 普通表单项
}else{
// the file:
// get the name;
String name = UUID.randomUUID().toString().replace("-","")+"_"+item.getName();
item.write(new File(path,name));
// 删除临时文件:
item.delete();
}
}
} catch (Exception e) {
e.printStackTrace();
}
return "success";
}
但是遇到了问题,返回list为空,图片上传成功了,但是没有被读取呜呜;
SpringMVC的方式:
bean配置:
<!-- 文件解析器 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="10485760"/><!-- 10MB -->
</bean>
index.jsp:
<h1>file uploading:fileUploadByMVC</h1>
<form action="response/fileUploadByMVC" method="post" enctype="multipart/form-data">
<label for="fileUpload">文件上传:</label><input name="upload" type="file" id="fileUpload1" value="click-to-upload"/>
<button type="submit">upload</button>
</form>
source code:
@RequestMapping("/fileUploadByMVC")
public String fileUploadByMVC(HttpServletRequest req, MultipartFile upload) throws IOException {
System.out.println("MVC文件上传成功。。。。。。");
String path = req.getSession().getServletContext().getRealPath("/uploads/");
File newFile = new File(path);
if(!newFile.exists()){
newFile.mkdirs();
}
String post_file = upload.getName();
String pre_name = UUID.randomUUID().toString().replace("-","")+"_";
String fileName = pre_name+post_file;
upload.transferTo(new File(path,fileName));
return "success";
}
跨服务器:
依赖:
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-core</artifactId>
<version>1.18.6</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-client</artifactId>
<version>1.18.6</version>
遇到问题:404not found:(13条消息) SpringMVC跨服务器上传文件出现returned a response status of 400 OR 403 OR 404 OR 409_chpllp的博客-CSDN博客
405错误:多半是tomcat下的配置,不允许,在conf路径下的web.xml中找到default的servlet 进行配置就可以啦;
<init-param>
<param-name>readonly</param-name>
<param-value>false</param-value>
</init-param>
<!-- tomcat-config-web.xml -->
发送方:
源码:client 的使用;
@RequestMapping("/fileUploadByClient")
public String fileUploadByClient(MultipartFile upload) throws IOException {
System.out.println("Client文件上传成功。。。。。。");
String path = "http://localhost:9090/uploads/";
String post_file = upload.getOriginalFilename();// the initial name;
String pre_name = UUID.randomUUID().toString().replace("-","")+"_";
String fileName = pre_name+post_file;
// get the client:
Client client = Client.create();
WebResource webResource = client.resource(path + fileName);
webResource.put(upload.getBytes());
// upload.transferTo(new File(path,fileName));
return "success";
}
目的服务器的一些注意配置:
over!