百度一下,知道是路径出了问题,找了好久原来是在拼接地址的时候忘记加**/**了
错误:String filePath = “c:/images”+name;
正确:String filePath = “c:/images/”+name;
这是构建一个简单的
文件上传
:
jsp代码如下:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>实现文件长传</h1>
<!--enctype="开启多媒体标签" -->
<form action="http://localhost/file" method="post"
enctype="multipart/form-data">
<input name="fileImage" type="file" />
<input type="submit" value="提交"/>
</form>
</body>
</html>
控制层的代码如下:
@RestController
public class FileController {
@Autowired
private FileService fileService;
/**
* MultipartFile:专门处理文件上传的
* @param fileImage
* @return
* @throws IOException
* @throws IllegalStateException
*/
@RequestMapping("/file")
public String file( MultipartFile fileImage) throws IllegalStateException, IOException {
//1.准备文件目录
File fileDir = new File("c:/images");
if (!fileDir.exists()) {
fileDir.mkdirs();
}
//2.获取文件的名称
String name = fileImage.getOriginalFilename();
//3.准备文件的路径
String filePath = "c:/images/"+name;
//4.实现文件的上传
fileImage.transferTo(new File(filePath));
System.out.println("上传成功!!!!!!!!!!!!");
return "文件上传成功@_@";
}