1. 新建MavenProject項(xiàng)目:SpringMVCUploadFileDemo,消除錯(cuò)誤。
package cc.kk.action;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Controller;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
@Controller
public class UploadFileAction {
/**
* 1 最原始的輸入輸出流復(fù)制文件
* SpringMVC 用的是 的MultipartFile來進(jìn)行文件上傳 所以我們首先要配置MultipartResolver:用于處理表單中的file
442毫秒搞定圖片傳輸
*/
@RequestMapping("parserUploadFile1")
public String parserUploadFile1(MultipartFile file) throws IOException{
System.out.println(new Date().getTime());
String realPath = "D:/text/";
// getInputStream()文件數(shù)據(jù)為輸入流
InputStream is = file.getInputStream();
//getOriginalFilename()是得到上傳時(shí)的文件名
//根據(jù)路徑創(chuàng)建文件輸出流并重命名(采用當(dāng)時(shí)時(shí)間+原文件名的方式)
FileOutputStream os = new FileOutputStream(realPath + new Date().getTime() + file.getOriginalFilename());
int i = 0; //標(biāo)記數(shù)
while(( i = is.read()) != -1){ //is.read()為-1時(shí)表示到了文件末尾
os.write(i);
}
os.flush();
os.close();
is.close();
System.out.println(new Date().getTime());
return "success";
}
/**
* 使用apache自帶的工具(api)FileUtils工具類進(jìn)行復(fù)制
* 13毫秒搞定圖片傳輸
* @param file
* @return
* @throws IOException
*/
@RequestMapping("parserUploadFile2")
public String uploadFile2(MultipartFile file) throws IOException{
System.out.println(new Date().getTime());
String realPath = "D:/text/";
FileUtils.copyInputStreamToFile(file.getInputStream(), new File(realPath,file.getOriginalFilename()));
System.out.println(new Date().getTime());
return "success";
}
/**
* 通過springmvc的API上傳
* 3毫秒搞定單張圖片傳輸(速度最快,優(yōu)先使用)
*/
@RequestMapping("parserUploadFile3")
public String uploadFile3(MultipartFile file) throws IOException{
System.out.println(new Date().getTime());
String readPath = "D:/text/";
System.out.println(1);
file.transferTo(new File(readPath,file.getOriginalFilename()));
//file2.transferTo(new File(readPath,file2.getOriginalFilename()));
System.out.println(new Date().getTime());
return "success";
}
/**
* 單張圖片上傳的另一種方法
* @param req
* @return
* @throws Exception
*/
@RequestMapping("parserUploadFile4")
public String uploadFile4(HttpServletRequest req) throws Exception{
MultipartHttpServletRequest mreq = (MultipartHttpServletRequest)req;
MultipartFile file = mreq.getFile("file");
System.out.println(new Date().getTime());
String readPath = "D:/text/";
file.transferTo(new File(readPath,file.getOriginalFilename()));
System.out.println(new Date().getTime());
return "success";
}
/**
* 通過springmvc的API上傳,實(shí)現(xiàn)多文件上傳
* 速度最快的多張圖片上傳
* @param req
* @return
*/
@RequestMapping("parserUploadFile5")
public String upLoadFile5(MultipartHttpServletRequest req){
System.out.println(new Date().getTime()); //用于比較傳輸速度(開始)
MultiValueMap<String,MultipartFile> map = req.getMultiFileMap(); //必須
List<MultipartFile> list = map.get("file");
String readPath = "D:/text/"; //文件存儲(chǔ)路徑
for(MultipartFile mFile : list){
try {
mFile.transferTo(new File(readPath,mFile.getOriginalFilename())); //關(guān)鍵代碼
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println(new Date().getTime()); //用于比較傳輸速度(結(jié)束)
return "success";
}
}