1、准备工作有springmvc环境
2、加载maven包
<!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
3、配置spring-mvc
<!-- 文件上传bean -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" />
4、interface
package com.spring.myfileutilsinterface;
import java.io.IOException;
import org.springframework.web.multipart.MultipartFile;
public interface IFileUpDownLoadInterface {
public boolean fileUpload(MultipartFile mf,String path,String name)throws IllegalStateException, IOException ;
}
5、implements
package com.spring.myfileutilsimplements;
import java.io.File;
import java.io.IOException;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import com.spring.myfileutilsinterface.IFileUpDownLoadInterface;
@Service("fileUpDownLoadImpl")
public class FileUpDownLoadImpl implements IFileUpDownLoadInterface {
/**
* 简单的上传文件
* mf 文件
* path 文件路径
* name 必须有后缀
* @throws IOException
* @throws IllegalStateException
*/
public boolean fileUpload(MultipartFile mf, String path,String name) throws IllegalStateException, IOException {
File filePath = null;
//判断文件是否为空
if(!mf.isEmpty()) {
//判断path 是不是为null
if(path!=null && path.length() != 0) {
filePath = new File(path,name);
}
}
if(filePath == null) {
return false;
}
//判断文件夹是否存在 如果不存在的话 需要新建
if(!filePath.getParentFile().exists()) {
filePath.getParentFile().mkdirs();
}
mf.transferTo(filePath);
return true;
}
}
6、controller
package com.spring.test.controller;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
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.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import com.spring.myfileutilsinterface.IFileUpDownLoadInterface;
@Controller
public class FileUpDownLoadController {
@Autowired
@Qualifier("fileUpDownLoadImpl")
private IFileUpDownLoadInterface fileUpDownLoadImpl;
//发布文章
@RequestMapping(value = "upload", method = { RequestMethod.POST })
@CrossOrigin
@ResponseBody
public String upload(@RequestParam("file") MultipartFile file,@RequestParam("filename") String filename,HttpServletRequest request){
String path ="/home/jackray/soft/temp";
try {
fileUpDownLoadImpl.fileUpload(file, path, filename);
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
7.前端
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<form action="upload" enctype="multipart/form-data" method="post">
<table>
<tr>
<td>文件名称</td>
<td><input type="text" name="filename"></td>
</tr>
<tr>
<td>请选择文件</td>
<td><input type="file" name="file"></td>
</tr>
<tr>
<td><input type="submit" value="上传"></td>
</tr>
</table>
</form>
</body>
</html>