小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

SpringMvc實(shí)現(xiàn)上傳圖片的三種方法學(xué)習(xí)

 然并卵書(shū)屋 2017-08-25
Eclipse中項(xiàng)目名為:SpringMVCUploadFileDemo
步驟:
1. 新建MavenProject項(xiàng)目:SpringMVCUploadFileDemo,消除錯(cuò)誤。
2.導(dǎo)包:
只記得導(dǎo)apring-webmvc.3.2.8包,其它等報(bào)錯(cuò)后再說(shuō)差那個(gè)包了。
上傳圖片還需要commons-fileupload-1.2.1.jar;commons-io-2.2.jar;commons-logging-1.1.jar 三個(gè)包,從阿里云下載,用了個(gè)稍微新一點(diǎn)的包,希望不要出錯(cuò)。
在導(dǎo)入json包的時(shí)候,怎么也找不到j(luò)son-2.4包, 百度了一下,用了博文中的方法,導(dǎo)入了jackson-annotations-2.5.0.jar, jackson-core-2.5.0.jar, jackson-databind-2.5.0.jar三外包,(博文名:Spring mvc4使用json包變更,已存圖書(shū)館)。不知道能不能用。還是那個(gè)態(tài)度,發(fā)現(xiàn)問(wèn)題,再解決問(wèn)題。
初步導(dǎo)包完成,沒(méi)有報(bào)錯(cuò)。
3.配置applicationContext.xml,web.xml文件
學(xué)到新知識(shí):在applicationContext.xml中學(xué)到'訪問(wèn)靜態(tài)資源’,'上傳文件大小攔截’。
JAVA后臺(tái)代碼:

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來(lái)進(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工具類(lèi)進(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";
}
/**
* 通過(guò)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";
}
/**
* 通過(guò)springmvc的API上傳,實(shí)現(xiàn)多文件上傳
* 速度最快的多張圖片上傳
* @param req
* @return
*/
@RequestMapping("parserUploadFile5")
public String upLoadFile5(MultipartHttpServletRequest req){
System.out.println(new Date().getTime());  //用于比較傳輸速度(開(kāi)始)
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";
}
}

頁(yè)面代碼:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www./TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>上傳文件</title>
</head>
<body>
第一種解析上傳文件的方法<hr/>
<form action="parserUploadFile5" method="post" enctype="multipart/form-data">
<!-- <input type="file" name = "file" /> -->
<input type="file" name = "file" multiple="multiple" size="2"/>
<input type = "submit" value="上傳">
</form>
</body>
</html>


    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶(hù)發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買(mǎi)等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶(hù) 評(píng)論公約