
文章插圖
一、什么是fileUpload?
fileUpload是apache的commons組件提供的上傳組件,它最主要的工作就是幫我們解析request.getInpustream() ??梢詤⒖荚诰€API文檔:http://tool.oschina.net/apidocs/apidoc?api=commons-fileupload
二、fileupload組件工作原理
三、fileupload核心API
1. DiskFileItemFactory
構(gòu)造器
1) DiskFileItemFactory() // 使用默認(rèn)配置
2) DiskFileItemFactory(int sizeThreshold, File repository)
sizeThreshold 內(nèi)存緩沖區(qū), 不能設(shè)置太大, 否則會(huì)導(dǎo)致JVM崩潰
repository 臨時(shí)文件目錄
2. ServletFileUpload
1) isMutipartContent(request) // 判斷上傳表單是否為multipart/form-data類型 true/false
2) parseRequest(request) // 解析request, 返回值為L(zhǎng)ist<FileItem>類型
3) isFormField() //是否是普通文件
4) setFileSizeMax(long) // 上傳文件單個(gè)最大值 fileupload內(nèi)部通過(guò)拋出異常的形式處理, 處理文件大小超出限制, 可以通過(guò)捕獲這個(gè)異常, 提示給用戶
5) setSizeMax(long) // 上傳文件總量最大值
6) setHeaderEncoding(String) // 設(shè)置編碼格式
四、實(shí)現(xiàn)過(guò)程
1.導(dǎo)入jar包
2.編寫(xiě)jsp
3.編寫(xiě)servlet
//創(chuàng)建業(yè)務(wù)層對(duì)象
NewsService newsService = new NewsService();
InputStream in = null;
OutputStream out = null;
int id = 0;//頁(yè)面?zhèn)鱽?lái)的id值
//創(chuàng)建解析器工廠
DiskFileItemFactory factory = new DiskFileItemFactory();
//獲取解析器
ServletFileUpload upload = new ServletFileUpload(factory);
// 上傳表單是否為multipart/form-data類型
if(!upload.isMultipartContent(request)) {
return ;
}
//解析request的輸入流
try {
List<FileItem> parseRequest = upload.parseRequest(request);
//迭代list
for(FileItem f:parseRequest) {
if(f.isFormField()) {
//普通字段
id = Integer.parseInt(f.getFieldName());
String value = https://www.520longzhigu.com/diannao/f.getString();
System.out.println(“name”+”=”+value);
}else {
//上傳文件
//獲取上傳文件名
String name = f.getName();
System.out.println(“文件名”+name);
name = name.substring(name.lastIndexOf(“\”)+1);
System.out.println(name);
//獲取輸入流
in = f.getInputStream();
//獲取上傳文件路徑
String savePath = “D:\workspacedt91\FileUpLoadTestDemo\WebContent\images\”+name;
//上傳文件名若不存在, 則先創(chuàng)建
File path = new File(savePath);
if(!path.exists()) {
path.getParentFile().mkdir();
}
//獲取輸出流
out = new FileOutputStream(path);
int len = 0;
byte[] b = new byte[1024];
while((len = in.read(b)) > 0) {
out.write(b,0,len);
}
System.out.println(“上傳成功”);
//保存到數(shù)據(jù)庫(kù)
int count = newsService.saveUrl(name, id);
if(count > 0 ) {
System.out.println(“路徑保存成功”);
}else {
System.out.println(“路徑保存失敗”);
}
}
}
} catch (FileUploadException e) {
// TODO Auto-generated catch block
System.out.println(“上傳失敗”);
e.printStackTrace();
}finally {
if(in != null) {
in.close();
}
if(out != null) {
out.close();
}
}
以上關(guān)于本文的內(nèi)容,僅作參考!溫馨提示:如遇健康、疾病相關(guān)的問(wèn)題,請(qǐng)您及時(shí)就醫(yī)或請(qǐng)專業(yè)人士給予相關(guān)指導(dǎo)!
「愛(ài)刨根生活網(wǎng)」www.malaban59.cn小編還為您精選了以下內(nèi)容,希望對(duì)您有所幫助:- 阿里網(wǎng)盤(pán)優(yōu)質(zhì)資源分享 阿里云管理控制臺(tái)
- 寫(xiě)作賺錢(qián)的三大渠道分享,在家碼字就能掙錢(qián),適合所有人 教您網(wǎng)上打字賺錢(qián)
- jquery獲取元素屬性的值 jq獲取自定義屬性的方法
- 光遇怎么快速獲取集結(jié)季橙色蠟燭 光遇獲取橙色蠟燭方法
- js獲取div的value值 js獲取div的值
- jquery獲取input的value值的方法 jquery獲取input的value值
- 域名注冊(cè)流程分享 阿里云注冊(cè)域名的步驟
- 玩友分享金剛菩子陰干后清理之盤(pán)與
- 棕色怎么調(diào)黑色 黑色怎么調(diào)
- 拿來(lái)吧你是什么梗?拿來(lái)吧你含義出處介紹 拿來(lái)吧你表情包分享
