JavaWeb 文件的上傳和下載功能簡(jiǎn)單實(shí)現(xiàn)代碼
一、文件的上傳和下載
1、文件上傳的原理分析
1、文件上傳的必要前提:
a、提供form表單,method必須是post
b、form表單的enctype必須是multipart/form-data
c、提供input type="file"類的上傳輸入域
2、enctype屬性
作用:告知服務(wù)器請(qǐng)求正文的MIME類型(請(qǐng)求消息頭:Content-Type作用是一致的)
可選值:
application/x-www-form-urlencoded(默認(rèn)):
正文:name=admin&password=123;
服務(wù)器獲取數(shù)據(jù):String name = request.getParameter("name");
multipart/form-data:
服務(wù)器獲取數(shù)據(jù):request.getParameter(String)方法獲取指定的表單字段字符內(nèi)容,但文件上傳表單已經(jīng)不再是字符內(nèi)容,而是字節(jié)內(nèi)容,所以失效。
文件上傳:解析請(qǐng)求正文的每部分的內(nèi)容
<body>
<form enctype="multipart/form-data" action="${pageContext.request.contextPath }/servlet/uploadServlet2" method="post" >
<input type="text" name="name"/><br/>
<input type="file" name="photo"/><br/>
<input type="submit" value="上傳"/><br/>
</form>
</body>
public class UploadServlet1 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
/*
* 由于表單中提交數(shù)據(jù)的方式改為multipart/form-data,所以request.getParameter("name")失效
* String name = request.getParameter("name");
String photo = request.getParameter("photo");
System.out.println(name);
System.out.println(photo);*/
InputStream is = request.getInputStream();
int len = 0;
byte[] b = new byte[1024];
while((len=is.read(b))!=-1){
System.out.println(new String(b,0,len));
}
is.close();
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
2、借助第三方的上傳組件實(shí)現(xiàn)文件上傳
1、fileupload概述
fileupload是由apache的commons組件提供的上傳組件。它最主要的工作就是幫我們解析request.getInputStream()
導(dǎo)入commons-fileupload相關(guān)jar包:
commons-fileupload.jar 核心包
commons-io.jar 依賴包
2、fileupload的核心類:
DiskFileItemFactory、ServletFileUpload、FileItem
a、解析原理
3、fileupload簡(jiǎn)單應(yīng)用
使用fileupload組件的步驟如下:
1、創(chuàng)建工廠類DiskFileItemFactory對(duì)象:
DiskFileItemFactory factory = new DiskFileItemFactory();
2、使用工廠創(chuàng)建解析器對(duì)象:
ServletFileUpload fileUpload = new ServletFileUpload(factory);
3、使用解析器來(lái)解析request對(duì)象:
List<FileItem> list = fileUpload.parseRequest(request)
FileItem對(duì)象對(duì)應(yīng)一個(gè)表單項(xiàng)(表單字段)??梢允俏募侄位蚱胀ㄗ侄?br />
boolean isFormField():判斷當(dāng)前表單字段是否為普通文本字段,如果返回false,說(shuō)明是文件字段
String getFieldName():獲取字段名稱,例如:<input type="text" name="username" /> 返回的是username
String getString():獲取字段的內(nèi)容,如果是文件字段,那么獲取的是文件內(nèi)容,當(dāng)然上傳的文件必須是文本文件
String getName():獲取文件字段的文件名稱(a.txt)
String getContentType():獲取上傳的文件的MIME類型,例如:text/plain
int getSize():獲取上傳文件的大小
InputStream getInputStream():獲取上傳文件對(duì)應(yīng)的輸入流
void write(File):把上傳的文件保存到指定文件中
delete()
3、文件上傳時(shí)需要考慮的幾個(gè)問(wèn)題(經(jīng)驗(yàn)分享)
1、保證服務(wù)器的安全
把保存上傳文件的目錄放在用戶直接訪問(wèn)不到的地方
2、避免文件被覆蓋
讓文件名唯一即可
3、避免同一個(gè)文件夾中的文件過(guò)多
方案一:按照日期進(jìn)行打散存儲(chǔ)目錄
方案二:用文件名的hashcode計(jì)算打散的存儲(chǔ)目錄:二級(jí)目錄
4、限制文件的大?。簑eb方式不適合上傳大的文件
單個(gè)文件大小:
ServletFileUpload.setFileSizeMax(字節(jié))
總文件大?。?多文件上傳)
ServletFileUpload.setSizeMax(字節(jié))
5、上傳字段用戶沒(méi)有上傳的問(wèn)題:
通過(guò)判斷文件名是否為空即可
6、臨時(shí)文件的問(wèn)題:
DiskFileItemFactory:
作用:產(chǎn)生FileItem對(duì)象
內(nèi)部有一個(gè)緩存,緩存大小默認(rèn)是10kb,如果上傳的文件超過(guò)10kb,用磁盤作為緩存。
存放緩存文件的目錄在哪里?默認(rèn)是系統(tǒng)的臨時(shí)目錄。
如果自己用IO流實(shí)現(xiàn)的文件上傳,要在流關(guān)閉后,清理臨時(shí)文件。
FileItem.delete();
4、
public class UploadServlet2 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// request.setCharacterEncoding("UTF-8");
// 要執(zhí)行文件上傳的操作
// 判斷表單是否支持文件上傳。即:enctype="multipart/form-data"
boolean isMultipartContent = ServletFileUpload
.isMultipartContent(request);
if (!isMultipartContent) {
throw new RuntimeException("your form is not multipart/form-data");
}
// 創(chuàng)建一個(gè)DiskFileItemfactory工廠類
DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setRepository(new File("f:\\"));// 指定臨時(shí)文件的存儲(chǔ)目錄
// 創(chuàng)建一個(gè)ServletFileUpload核心對(duì)象
ServletFileUpload sfu = new ServletFileUpload(factory);
// 解決上傳表單項(xiàng)亂碼問(wèn)題
sfu.setHeaderEncoding("UTF-8");
// 限制上傳文件的大小
// 解析request對(duì)象,并得到一個(gè)表單項(xiàng)的集合
try {
// sfu.setFileSizeMax(1024*1024*3);//表示3M大小
// sfu.setSizeMax(1024*1024*6);
List<FileItem> fileItems = sfu.parseRequest(request);
// 遍歷表單項(xiàng)數(shù)據(jù)
for (FileItem fileitem : fileItems) {
if (fileitem.isFormField()) {
// 普通表單項(xiàng)
processFormField(fileitem);
} else {
// 上傳表單項(xiàng)
processUploadField(fileitem);
}
}
} catch (FileUploadBase.FileSizeLimitExceededException e) {
// throw new RuntimeException("文件過(guò)在,不能超過(guò)3M");
System.out.println("文件過(guò)在,不能超過(guò)3M");
} catch (FileUploadBase.SizeLimitExceededException e) {
System.out.println("總文件大小不能超過(guò)6M");
} catch (FileUploadException e) {
e.printStackTrace();
}
}
private void processUploadField(FileItem fileitem) {
try {
// 得到文件輸入流
InputStream is = fileitem.getInputStream();
// 創(chuàng)建一個(gè)文件存盤的目錄
String directoryRealPath = this.getServletContext().getRealPath(
"/WEB-INF/upload");
File storeDirectory = new File(directoryRealPath);// 即代表文件又代表目錄
if (!storeDirectory.exists()) {
storeDirectory.mkdirs();// 創(chuàng)建一個(gè)指定的目錄
}
// 得到上傳的名子
String filename = fileitem.getName();// 文件項(xiàng)中的值 F:\圖片素材\小清新\43.jpg 或者
// 43.jpg
if (filename != null) {
// filename =
// filename.substring(filename.lastIndexOf(File.separator)+1);
filename = FilenameUtils.getName(filename);// 效果同上
}
// 解決文件同名的問(wèn)題
filename = UUID.randomUUID() + "_" + filename;
// 目錄打散
// String childDirectory = makeChildDirectory(storeDirectory); //
// 2015-10-19
String childDirectory = makeChildDirectory(storeDirectory, filename); // a/b
// 上傳文件,自動(dòng)刪除臨時(shí)文件
fileitem.write(new File(storeDirectory, childDirectory
+ File.separator + filename));
fileitem.delete();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
// 上傳表單項(xiàng)
private void processUploadField1(FileItem fileitem) {
try {
// 得到文件輸入流
InputStream is = fileitem.getInputStream();
// 創(chuàng)建一個(gè)文件存盤的目錄
String directoryRealPath = this.getServletContext().getRealPath(
"/WEB-INF/upload");
File storeDirectory = new File(directoryRealPath);// 即代表文件又代表目錄
if (!storeDirectory.exists()) {
storeDirectory.mkdirs();// 創(chuàng)建一個(gè)指定的目錄
}
// 得到上傳的名子
String filename = fileitem.getName();// 文件項(xiàng)中的值 F:\圖片素材\小清新\43.jpg 或者
// 43.jpg
// 處理文件名
/*
* 解決此問(wèn)題: F:\\apache-tomcat-7.0.52\\webapps\\day18_00_upload\\upload\\F:\\圖片素材\\小清新\\41.jpg
*/
if (filename != null) {
// filename =
// filename.substring(filename.lastIndexOf(File.separator)+1);
filename = FilenameUtils.getName(filename);// 效果同上
}
// 解決文件同名的問(wèn)題
filename = UUID.randomUUID() + "_" + filename;
// 目錄打散
// String childDirectory = makeChildDirectory(storeDirectory); //
// 2015-10-19
String childDirectory = makeChildDirectory(storeDirectory, filename); // 2015-10-19
// 在storeDirectory下,創(chuàng)建完整目錄下的文件
File file = new File(storeDirectory, childDirectory
+ File.separator + filename); // 絕對(duì)目錄/日期目錄/文件名
// 通過(guò)文件輸出流將上傳的文件保存到磁盤
FileOutputStream fos = new FileOutputStream(file);
int len = 0;
byte[] b = new byte[1024];
while ((len = is.read(b)) != -1) {
fos.write(b, 0, len);
}
fos.close();
is.close();
fileitem.delete();
} catch (IOException e) {
e.printStackTrace();
}
}
// 目錄打散
private String makeChildDirectory(File storeDirectory, String filename) {
int hashcode = filename.hashCode();// 返回字符轉(zhuǎn)換的32位hashcode碼
System.out.println(hashcode);
String code = Integer.toHexString(hashcode); // 把hashcode轉(zhuǎn)換為16進(jìn)制的字符
// abdsaf2131safsd
System.out.println(code);
String childDirectory = code.charAt(0) + File.separator
+ code.charAt(1); // a/b
// 創(chuàng)建指定目錄
File file = new File(storeDirectory, childDirectory);
if (!file.exists()) {
file.mkdirs();
}
return childDirectory;
}
// 按日期打散
/*
* private String makeChildDirectory(File storeDirectory) {
*
* SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd"); String
* dateDirectory = sdf.format(new Date()); //只管創(chuàng)建目錄 File file = new
* File(storeDirectory,dateDirectory); if(!file.exists()){ file.mkdirs(); }
*
* return dateDirectory; }
*/
// 普通表單項(xiàng)
private void processFormField(FileItem fileitem) {
try {
String fieldname = fileitem.getFieldName();// 字段名
String fieldvalue = fileitem.getString("UTF-8");// 字段值
//fieldvalue = new String(fieldvalue.getBytes("iso-8859-1"),"utf-8");
System.out.println(fieldname + "=" + fieldvalue);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
5、動(dòng)態(tài)添加多個(gè)文件上傳 jsp
<script type="text/javascript">
function addFile(){
//得到div容器
var div1 = document.getElementById("div1");
div1.innerHTML += "<div><input type='file' name='photo' /><input type='button' value='刪除' onclick='delFile(this)'/><br /></div>";
}
function delFile(input){
//使用input對(duì)象的爺爺刪除他的爸爸
input.parentNode.parentNode.removeChild(input.parentNode);
}
</script>
<body>
<form enctype="multipart/form-data"
action="${pageContext.request.contextPath }/servlet/uploadServlet2"
method="post">
<input type="text" name="name" /><br />
<div id="div1">
<div>
<input type="file" name="photo" /><input type="button" value="添加" onclick="addFile()"/><br />
</div>
</div>
<input type="submit" value="上傳" /><br />
</form>
</body>
6、下載文件
public class DownloadServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//設(shè)置一個(gè)要下載的文件
String filename = "銷售榜單.csv";
//設(shè)置文件名的編碼
if(request.getHeader("user-agent").toLowerCase().contains("msie")){
filename = URLEncoder.encode(filename, "UTF-8");//將不安全的文件名改為UTF-8格式
}else{
filename = new String(filename.getBytes("UTF-8"),"iso-8859-1");//火狐瀏覽器
}
//告知瀏覽器要下載文件
response.setHeader("content-disposition", "attachment;filename="+filename);
//response.setHeader("content-type", "image/jpeg");
response.setContentType(this.getServletContext().getMimeType(filename));//根據(jù)文件名自動(dòng)獲得文件類型
response.setCharacterEncoding("UTF-8");//告知服務(wù)器使用什么編碼
//創(chuàng)建一個(gè)文件輸出流
PrintWriter out = response.getWriter();
out.write("電視機(jī),20\n");
out.write("洗衣機(jī),10\n");
out.write("冰箱,8\n");
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
以上所述是小編給大家介紹的JavaWeb 文件的上傳和下載功能簡(jiǎn)單實(shí)現(xiàn)代碼,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
解決Error:(1,?1)?java:?非法字符:?'\ufeff'問(wèn)題
這篇文章主要介紹了解決Error:(1,?1)?java:?非法字符:?'\ufeff'問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11
SpringBoot使用Nacos配置中心的實(shí)現(xiàn)
這篇文章主要介紹了SpringBoot使用Nacos配置中心的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12
Java實(shí)現(xiàn)簡(jiǎn)易的洗牌和發(fā)牌功能
本文主要介紹了Java實(shí)現(xiàn)簡(jiǎn)易的洗牌和發(fā)牌功能,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04
Java?數(shù)據(jù)結(jié)構(gòu)與算法系列精講之單向鏈表
單向鏈表特點(diǎn)是鏈表的鏈接方向是單向的,訪問(wèn)要通過(guò)順序讀取從頭部開始。鏈表是使用指針構(gòu)造的列表,是由一個(gè)個(gè)結(jié)點(diǎn)組裝起來(lái)的,又稱為結(jié)點(diǎn)列表。其中每個(gè)結(jié)點(diǎn)都有指針成員變量指向列表中的下一個(gè)結(jié)點(diǎn),head指針指向第一個(gè)結(jié)點(diǎn)稱為表頭,而終止于最后一個(gè)指向nuLL的指針2022-02-02
解決nacos項(xiàng)目啟動(dòng)報(bào)錯(cuò):Connection refused: no further&
這篇文章主要介紹了解決nacos項(xiàng)目啟動(dòng)報(bào)錯(cuò):Connection refused: no further informa問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04
Java中的構(gòu)造方法(構(gòu)造函數(shù))與普通方法區(qū)別及說(shuō)明
這篇文章主要介紹了Java中的構(gòu)造方法(構(gòu)造函數(shù))與普通方法區(qū)別及說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03

