上傳文件

今天我們學的是文件上傳,下面是文件上傳的概述:

實現web開發中的文件上傳功能,需完成如下二步操作:
web頁面中添加上傳輸入項
servlet中讀取上傳文件的數據,並保存到本地硬盤中。
如何在web頁面中添加上傳輸入項?
<input type=file>標籤用於在web頁面中添加文件上傳輸入項,設置文件上傳輸入項時須注意:
1、必須要設置input輸入項的name屬性,否則瀏覽器將不會發送上傳文件的數據。
2、必須把formenctype屬值設爲multipart/form-data.設置該值後,瀏覽器在上傳文件時,將把文件數據附帶在http請求消息體中,並使用MIME協議對上傳的文件進行描述,以方便接收方對上傳數據進行解析和處理。

如何在Servlet中讀取文件上傳數據,並保存到本地硬盤中?
Request對象提供了一個getInputStream方法,通過這個方法可以讀取到客戶端提交過來的數據。但由於用戶可能會同時上傳多個文件,在servlet端編程直接讀取上傳數據,並分別解析出相應的文件數據是一項非常麻煩的工作,示例。
爲方便用戶處理文件上傳數據,Apache 開源組織提供了一個用來處理表單文件上傳的一個開源組件( Commons-fileupload ),該組件性能優異,並且其API使用極其簡單,可以讓開發人員輕鬆實現web文件上傳功能,因此在web開發中實現文件上傳功能,通常使用Commons-fileupload組件實現。
使用Commons-fileupload組件實現文件上傳,需要導入該組件相應的支撐jar包:Commons-fileuploadcommons-iocommons-io 不屬於文件上傳組件的開發jar文件,但Commons-fileupload 組件從1.1 版本開始,它工作時需要commons-io包的支持。

首先是文件上傳的基本操作:

1、表單屬性enctype的設置

multipart/form-dataapplication/x-www-form-urlencoded的區別:

FORM元素的enctype屬性指定了表單數據向服務器提交時所採用的編碼類型,默認的缺省值是

application/x-www-form-urlencoded

然而,在向服務器發送大量的文本、包含非ASCII字符的文本或二進制數據時這種編碼方式效率很低。

在文件上載時,所使用的編碼類型應當是“multipart/form-data”,它既可以發送文本數據,也支持二進制數據上載。

Browser端<form>表單的ENCTYPE屬性值爲multipart/form-data,它告訴我們傳輸的數據要用到多媒體傳輸協議,由於多媒體傳輸的都是大量的數據,所以規定上傳文件必須是post方法,<input>的type屬性必須是file。

2、實現過程:

upload.jsp

<form action ="${pageContext.request.contextPath}/servlet/UploadServlet" enctype="multipart/form-data" method="post"/>

上傳用戶<input type="text" name="username" /><br/>

文件1<input type="file" name="file1" /><br/>

文件2<input type="file" name="file2" /><br/>

<input type="submit" value="submit" /><br/>

</form>

UploadServlet:

System.out.println(request.getParameter("username"));

InputStream in = request.getInputStream();

byte[] buffer = new byte[1024];

int len = 0;

while((len=in.read(buffer))>0){

System.out.println(new String(buffer));

}

二、Commoms FilesUpLoad

upload.jsp

<form action="${pageContext.request.contextPath}/servlet/UploadServlet2" enctype="multipart/form-data" method="post">

上傳用戶<input type="text" name="username" /><br/>

文件1<input type="file" name="file1" /><br/>

文件2<input type="file" name="file2" /><br/>

<input type="submit" value="submit" /><br/>

</form>

UploadServlet:

request.setCharacterEncoding("utf-8"); //對post有效

try{

//1 創建解析工廠

DiskFileItemFactory factory = new DiskFileItemFactory();

//2 獲取一個解析器

ServletFileUpload upload = new ServletFileUpload(factory);

//3 對請求對象進行解析

List<FileItem> list = upload.parseRequest(request);

//4 對FileItem對象列表進行迭代

for(FileItem item : list){

if(item.isFormField()){ //普通輸入項

String paramName = item.getFieldName();

String paramValue = item.getString();

//亂碼問題

paramValue=

new String(paramValue.getBytes("iso8859-1"),"utf-8");

System.out.println(paramName + " = " + paramValue);

}else{ //上傳文件

String fileName = item.getName();

System.out.println("filename = " + fileName);

fileName = fileName.substring(fileName.lastIndexOf("\\")+1);

System.out.println("filename = " + fileName);

InputStream in = item.getInputStream();

byte[] buffer = new byte[1024];

int len = 0;

FileOutputStream fos = new FileOutputStream("c:\\"+fileName);

while((len = in.read(buffer)) >0){

fos.write(buffer, 0, len);

}

fos.flush();

in.close();

fos.close();

request.setAttribute("message","上傳成功!!!");

}

}

}catch(Exception e){

e.printStackTrace();

request.setAttribute("message", "上傳失敗!!");

}

request.getRequestDispatcher("/message.jsp").

forward(request, response);

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章