首先自我介绍一下,我是王登高,下面是我的头像:
是不是很帅,我每天照镜子都感觉自己帅。
好了不扯这些没用的,我们进入正题:前两天,自己边学边做遇到的SpringMVC上传文件的问题,用户可以上传图片,然后显示出来。就是这样简单的需求,我搞了两天,我就常常在想是不是我有点笨,还是智商有问题。但是最后搞出来我还是无耻的安慰自己,不算太笨。可能大家并不知道我,我写博客已经很长时间的,但是你翻看可能会发现,都是一些比较基础的,但是我感觉自己涉及的范围还挺广的,比如说cocos2d,c,C++,java,php,javaweb前端之类,不少了吧,可以毫不骄傲的说,我精通很多语言。。。我就是这么拽,我常常告诉自己要拽一点,再拽一点。好了,不扯淡了,我们继续今天的SpringMVC的讲解。
大家看,这个是我的SpringMVC的工程目录:
看到了吗,在上面的图像中我们把upload标注了,标注了,我们现在要实现的功能就是从前台网页端上传图片到这个文件夹里面,那么怎么实现:
其实我们思路是很重要的,上传图片,其实比较重要的是前台一个上传控件,然后后台接收,利用io将图片写入到指定的文件夹。那么上传控件我们都知道直接利用input type='file'不就好了,对啊:
<form class="uploadfile" action="../../../filecontroller/upload" method="post" enctype="multipart/form-data">
<input type="file" name="files" accept="image/png, image/jpg"/>
<input type="file" name="files" accept="image/png, image/jpg"/>
<input type="file" name="files" accept="image/png, image/jpg"/>
<input id="submitfile" type="submit" value="Submit" />
</form>
上面就是我们前台的表单控件了,accept标识上传文件的类型,直接拿过去用都可以了,而且你是否发现,我们这个地方写了很多的input控件,对了你没看错,就是这么拽,我们要同时上传多个文件,在说一遍上传多个文件,当然你想仅仅上传一个,下面两个你自己删掉可好。上面是我们前端的,在SpringMVC中我们还需要干嘛,后台要接收啊,要不然文件传给谁啊,对不对,在看接收方之前,我们先看看配置文件应该怎么配置,下面是我的配置方式首先是保证web.xml能加载到applicantContent.xml这个配置文件:
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext.xml</param-value>
</context-param>
其实一开始编写代码的时候我根本就没注意到这个监听类,我们在一开始搭建SpringMVC的时候,一般都是网上的教程需要我们配置至少三个配置文件吧,一个web.xml,一个是SpringMVC-Servlet.xml这个配置文件,还有一个是applicantContent.xml一般刚开始我们是不太清楚这三个配置文件有怎样的作用,现在我感觉web.xml的作用似乎在于指定类和配置文件之间的关系。比如我们请求转发把类和SpringMVC-servlet.xml这个配置文件关联。然后这个地方文件加载的监听类和applicantContent.xml这个进行关联,好了就不扯这三个配置文件之间的关系了。我们文件上传功能在在在applicantContent中应该怎么配置:
<!-- 图片文件上传功能 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="10000000"/>
</bean>
文件解析类,对于这个里面一些属性的去参考其他的博客吧
如果你这些都配置好了,现在我们来看接口:
package com.wdg.controller;
import java.io.File;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.core.Context;
import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;
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.RestController;
import org.springframework.web.multipart.MultipartFile;
@RestController
@RequestMapping("/filecontroller")
public class UploadFileController {
private Logger log;
public UploadFileController(){
this.log = Logger.getLogger(this.getClass());
}
@RequestMapping(value = "/upload", method = RequestMethod.POST)
public String upload(@RequestParam("files") MultipartFile[] files,@Context HttpServletRequest request) {
try {
log.info("-------------------开始调用上传文件upload接口-------------------");
for (int i = 0; i < files.length; i++) {
System.out.println(files[i].getOriginalFilename());
//System.out.println( System.getProperty("user.dir"));
// String path1=this.getClass().getClassLoader().getResource(".").getPath();
String t=Thread.currentThread().getContextClassLoader().getResource("").getPath();
int num=t.indexOf("Shopping");
String path=t.substring(1,num).replace('/', '\\')+"Shopping\\WebContent\\resources\\upload\\";
System.out.println(path);
// System.out.println(path1);
/*System.out.println(path.substring(0, path.indexOf("Shopping")));*/
// FileUtils.writeByteArrayToFile(new File("e:/upload/" + files[i].getOriginalFilename()),
// files[i].getBytes());
FileUtils.writeByteArrayToFile(new File(path + files[i].getOriginalFilename()),
files[i].getBytes());
}
log.info("-------------------结束调用上传文件upload接口-------------------");
return "ok";
} catch (IOException e) {
e.printStackTrace();
return "error";
}
}
}
Shopping是我工程的名字,为什么我在这个地方shopping--》烧饼---》shangbing----》sb
好吧,回到主题,我不算傻吧
希望对你有所帮助!