一、文件上傳簡介
文件上傳在HTML中是以
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="upload1"/><br />
<input type="submit" value="上傳"/>
</form>
形式出現的。
文件上傳有兩種選擇:
(1)SmartUpload:以jar包形式出現,需要把他添加到classpath或tomcat的lib文件夾下。
(2)FileUpload:以jar包形式出現,需要把他添加到classpath或tomcat的lib文件夾下。注意:此包與common-io包是相互依賴的,因此需要同時存放。
對於SmartUpload,使用較方便,但是官方已經不能下載。
二、SmartUpload
1.一般代碼流程
SmartUpload smart = new SmartUpload();
smart.initialize(pageContext);
smart.upload(); //準備上傳
smart.save("file");
實現的功能是將上傳到的文件保存在/file文件夾下,並以同名進行保存。
2.表單注意事項
文件上傳規定:表單必須有enctype="multipart/form-data"這個屬性;因此表單是以二進制數據發送的,比如表單中有一個text,2個上傳控件,發送數據時是一起以二進制發送的。
3.獲取表單中其他普通控件的值
因爲有了文件上傳控件後,表單的其他控件傳遞數據不能通過普通的request.getParameter(),而需要smart.getRequest().getParameter();
4.自定義存儲文件名稱
String ext = smart.getFiles().getFile(0).getFileExt();// 獲得文件後綴
smart.getFiles().getFile(0).saveAs(this.getServletContext().getRealPath("/")+filename+"."+ext); // 另存爲自定義文件名
filename就是文件自定義名稱,ext就是文件擴展名。
5.隨機文件名
- package org.random;
- import java.util.*;
- import java.text.*;
- public class IPTimeStamp{
- private StringBuffer buf;
- private String ip;
- public IPTimeStamp(String ip){ //傳入參數request.getRemoteAddr();即可
- this.ip = ip;
- buf = new StringBuffer();
- }
- public String getRandomFileName(){ //取得文件隨即名
- SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmssSSS");
- String[] ipadd = ip.split("\\.");
- for(String ipa:ipadd){
- buf.append(ipa);
- }
- buf.append(sdf.format(new Date()));
- Random ran = new Random();
- for(int i=0;i<3;i++){
- buf.append(ran.nextInt(10));
- }
- return buf.toString();
- }
- }
6.批量上傳
for(int i=0;i<smart.getFiles().getCount();i++){
String ext = smart.getFiles().getFile( i ).getFileExt();
smart.getFiles().getFile( i ).saveAs(this.getServletContext().getRealPath("/")+filename+"."+ext);
}即可。
從以上看出,SmartUpload的代碼量不會特別多,比較方便。
三、FileUpload
FileUpload是apache的commons項目的子項目,需要下載jar包,注意:還要把commons-io.jar也下下來,因爲這兩個包是相互關聯的。
一般需要導入:
(1)org.apache.commons.fileupload.*;
(2)org.apache.commons.fileupload.servlet.*;
(3)org.apache.commons.fileupload.disk.*;
1.一般代碼流程
DiskFileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setFileSizeMax(1024*1024);//設置上傳文件的最大容量
List<FileItem>items = upload.parseRequest(request); //取得表單全部數據
含有文件上傳控件的表單是不能區分一般控件和上傳控件的,都作爲FileItem;
2.區分一般控件數據和文件上傳控件數據
通過item.isFormField()能夠判斷,如果返回true,則表示一般控件數據。
3.FileItem用法
item.getFieldName()返回域名稱。
(1)如果是一般控件,則item.getString()即可。
(2)如果是文件上傳控件,則包含一些方法
item.getName();取得上傳文件的名稱
item.getContentType();取得上傳文件的mime類型
longitem.getSize();取得上傳文件的大小
item.getInputStream();取得上傳文件的輸入流
4.保存文件
在SmartUpload中,只需要save函數即可,但是在FileUpload中,需要IO流。
InputStream input = item.getInputStream();
FileOutputStream output = new FileOutputStream("file.txt");
byte[] buf = new byte[1024];
int length = 0;
while((length=input.read(buf))!=-1){
output.write(buf,0,length);
}
input.close();
output.close();
即可。
5.取得文件後綴
String ext = item.getName().split("\\.")[1];
6.對於大文件
對於打文件,需要設置臨時文件夾,形式:
factory.setRepository("filename");