SpringMVC圖片上傳
阿新 • • 發佈:2018-01-14
io流 empty ext value utf mapping fileutil ann inf
一.form表單提交
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="/user/moreupload.action" method="post" enctype="multipart/form-data">
<input type="text" name="wenben" value=""/><br>
<input type="file" name="file" /><br>
<input type="file" name="file" /><br>
<input type="submit" value="文件上傳" />
</form>
</body>
</html>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
@RequestMapping("/user")
@Controller
public class duowenjianshangchuan {
/**
* file:傳入的參數名稱 MultipartFile:上傳文件的對象,它會標識文件的類型。
*
* @param multipartFile
* @return String : 返回簡寫視圖名稱
* @throws Exception
*/
//第一種方法:這個是多文件上傳,如果是一個文件的話,就
@RequestMapping("/moreupload")
public String upload(@RequestParam("file") MultipartFile[] multipartFile, HttpServletRequest request)
throws Exception {
String string = request.getParameter("wenben");
System.out.println(string);
for (MultipartFile myfile : multipartFile) {
if (myfile.isEmpty()) {
System.out.println("文件未上傳");
} else {
System.out.println("文件長度: " + myfile.getSize());
System.out.println("文件類型: " + myfile.getContentType());
System.out.println("文件名稱: " + myfile.getName());
System.out.println("文件原名: " + myfile.getOriginalFilename());
System.out.println("========================================");
// 如果用的是Tomcat服務器,則文件會上傳到\\%TOMCAT_HOME%\\webapps\\YourWebProject\\WEB-INF\\upload\\文件夾中
String realPath = request.getSession().getServletContext().getRealPath("/WEB-INF/upload");
System.out.println("上傳到tomcat服務器的地址" + realPath);
// 這裏不必處理IO流關閉的問題,因為FileUtils.copyInputStreamToFile()方法內部會自動把用到的IO流關掉,我是看它的源碼才知道的
// FileUtils.copyInputStreamToFile(myfile.getInputStream(), new File(realPath,
// myfile.getOriginalFilename()));
}
}
return "redirect:/html/success.html";
}
//第二種方法,沒有在方法的參數裏面寫 @RequestParam("file") MultipartFile[] multipartFile
@RequestMapping("/moreupload2")
public String upload2(HttpServletRequest request) throws Exception {
MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
List<MultipartFile> files = multiRequest.getFiles("file");
for (int i = 0; i < files.size(); i++) {
System.out.println(files.get(i).getOriginalFilename());
}
return "redirect:/html/success.html";
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
二.ajax異步提交(H5 FormDate)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>上傳圖片</title>
<script src="/jquery-3.2.1.js"></script>
</head>
<body>
<div>
<input type="file" class="easyui-linkbutton" id="excelFile" name="excelFile" size="20" maxlength="20"/>
<input type="button" class="easyui-linkbutton" id="upload" style="width:80px" value="上傳" />
</div>
</body>
<script>
$(document).ready(function(){
function ajaxFileUpload(){
var formData = new FormData();
formData.append(‘file‘,$("#excelFile")[0].files[0]); //將文件轉成二進制形式
$.ajax({
type:"post",
url:"http://localhost:8080/fileupload",
async:false,
contentType: false, //這個一定要寫
processData: false, //這個也一定要寫,不然會報錯
data:formData,
dataType:‘text‘, //返回類型,有json,text,HTML。這裏並沒有jsonp格式,所以別妄想能用jsonp做跨域了。
success:function(data){
alert(data);
},
error:function(XMLHttpRequest, textStatus, errorThrown, data){
alert(errorThrown);
}
});
}
$("#upload").click(function(){
ajaxFileUpload();
});
});
</script>
</html>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
@RequestMapping(value="fileupload", method=RequestMethod.POST,produces="text/html;charset=utf-8")
public void addPic(HttpServletResponse response,HttpServletRequest request,
@RequestParam(value="file", required=false) MultipartFile file) throws IOException{
System.out.println(file.getOriginalFilename());
response.getWriter().write("success");
response.setHeader("Access-Control-Allow-Origin", "*");
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
三.參考
https://www.cnblogs.com/emperorking/articles/6396956.html
SpringMVC圖片上傳