spring mvc MultipartFile file 单文件上传文件
1.pom.xml添加依赖
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
2.springmvc.xml 添加如下配置
<!-- 定义文件上传解析器 -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 设定默认编码 -->
<property name="defaultEncoding" value="UTF-8"></property>
<!-- 设定文件上传的最大值5MB,5*1024*1024 -->
<property name="maxUploadSize" value="5242880"></property>
</bean>
3.页面配置(jquery一定要引入)
<html>
<head>
<script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery-3.1.1.min.js"></script>
<script>
function select_file() {
$("#file").click();
}
function submit_file() {
var formData = new FormData();
formData.append('file', $('#file')[0].files[0]);
$.ajax({
url: "${pageContext.request.contextPath}/uploadfiles.do",//路径接口 自定义
type: "POST",
processData: false,
contentType: false,
data: formData,
success: function(d) {
console.log(d);
if(d=="1"){
alert("恭喜您,上传文件成功!")
}else{
alert("抱歉,上传文件失败!")
}
},
error: function() {
alert("请求出错啦")
}
});
}
</script>
</head>
<body style="font-size:30px;">
<div><button onclick="select_file()">文件上传</button></div>
<form id="fom1" style="display: none;"
method="post" enctype="multipart/form-data">
<input type="file" id="file" name="file" onchange="submit_file();" />
</form>
</body>
<script>
</script>
</html>
4.接口(上传到本地服务器)
@RequestMapping("/uploadfiles.do")
@ResponseBody
public String uploadfiles(MultipartFile file, HttpServletRequest request,@RequestParam HashMap<String,Object> map) {
// 判断文件是否为空
if (!file.isEmpty()) {
try {
String savePath = "/aa/";
//文件夹路径
String dirPath=request.getSession().getServletContext().getRealPath("/") + savePath;
//文件名称
String fileName=file.getOriginalFilename();
File dir=new File(dirPath,fileName);
if(!dir.exists()){ //判断文件夹路径是否存在
dir.mkdirs(); //不存在创建新的文件夹
}
/* // 文件保存路径(此路径是部署在tomcat中的绝对路径)获取Web项目的全路径
String filePath = request.getSession().getServletContext().getRealPath("/") + savePath
+ file.getOriginalFilename();*/
// 转存文件
file.transferTo(dir);
} catch (Exception e) {
e.printStackTrace();
}
return "1";
}else{
return "0";
}
}
二.上传到其它服务器
1.引入依赖
<!--跨服务器上传 -->
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-client</artifactId>
<version>1.19</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-core</artifactId>
<version>1.19</version>
</dependency>
2.tomcat 中conf文件夹下 下web.xml中添加
在(含在servlet标签中)
<servlet>
<init-param>
<param-name>readonly</param-name>
<param-value>false</param-value>
</init-param>
</servlet>
@RequestMapping("/upload.do")
@ResponseBody
public String upload(MultipartFile file, HttpServletRequest request,@RequestParam HashMap<String,Object> map) {
// 判断文件是否为空
if (!file.isEmpty()) {
try {
String fullPath="";
//创建jesy服务器,进行跨服务器上传
Client client = Client.create();
//服务器名称 + 端口号 + (webapps文件下的文件路径)+ 文件名称
fullPath += "http://127.0.0.1:8080" + "/aa/"+file.getOriginalFilename();
WebResource wr = client.resource(fullPath);
wr.put(file.getBytes());
} catch (Exception e) {
e.printStackTrace();
}
return "1";
}else{
return "0";
}
}
创作不易 顺手点个赞 我搞的也有劲!
快乐的吃干货