免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費(fèi)電子書(shū)等14項(xiàng)超值服

開(kāi)通VIP
文件上傳:SmartUpload和FileUpload


一、文件上傳簡(jiǎn)介


文件上傳在HTML中是以

<form action="" method="post" enctype="multipart/form-data">

<input type="file" name="upload1"/><br />

<input type="submit" value="上傳"/>

</form>

形式出現(xiàn)的。

文件上傳有兩種選擇:

(1)SmartUpload:以jar包形式出現(xiàn),需要把他添加到classpath或tomcat的lib文件夾下。

(2)FileUpload:以jar包形式出現(xiàn),需要把他添加到classpath或tomcat的lib文件夾下。注意:此包與common-io包是相互依賴(lài)的,因此需要同時(shí)存放。

對(duì)于SmartUpload,使用較方便,但是官方已經(jīng)不能下載。


二、SmartUpload


1.一般代碼流程


    SmartUpload smart = new SmartUpload();

    smart.initialize(pageContext);

    smart.upload(); //準(zhǔn)備上傳

    smart.save("file");

實(shí)現(xiàn)的功能是將上傳到的文件保存在/file文件夾下,并以同名進(jìn)行保存。


2.表單注意事項(xiàng)


文件上傳規(guī)定:表單必須有enctype="multipart/form-data"這個(gè)屬性;因此表單是以二進(jìn)制數(shù)據(jù)發(fā)送的,比如表單中有一個(gè)text,2個(gè)上傳控件,發(fā)送數(shù)據(jù)時(shí)是一起以二進(jìn)制發(fā)送的。


3.獲取表單中其他普通控件的值


因?yàn)橛辛宋募蟼骺丶螅韱蔚钠渌丶鬟f數(shù)據(jù)不能通過(guò)普通的request.getParameter(),而需要smart.getRequest().getParameter();


4.自定義存儲(chǔ)文件名稱(chēng)


String ext = smart.getFiles().getFile(0).getFileExt();// 獲得文件后綴

smart.getFiles().getFile(0).saveAs(this.getServletContext().getRealPath("/")+filename+"."+ext); // 另存為自定義文件名

filename就是文件自定義名稱(chēng),ext就是文件擴(kuò)展名。


5.隨機(jī)文件名

  1. package org.random;  
  2. import java.util.*;  
  3. import java.text.*;  
  4. public class IPTimeStamp{  
  5.     private StringBuffer buf;  
  6.     private String ip;  
  7.     public IPTimeStamp(String ip){    //傳入?yún)?shù)request.getRemoteAddr();即可  
  8.         this.ip = ip;  
  9.         buf = new StringBuffer();  
  10.     }  
  11.     public String getRandomFileName(){   //取得文件隨即名  
  12.         SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmssSSS");  
  13.         String[] ipadd = ip.split("\\.");  
  14.         for(String ipa:ipadd){  
  15.             buf.append(ipa);  
  16.         }  
  17.         buf.append(sdf.format(new Date()));  
  18.         Random ran = new Random();  
  19.         for(int i=0;i<3;i++){  
  20.             buf.append(ran.nextInt(10));  
  21.         }  
  22.         return buf.toString();  
  23.     }  
  24. }  



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的代碼量不會(huì)特別多,比較方便。


三、FileUpload



模板代碼:

  1. boolean isMultipart = ServletFileUpload.isMultipartContent(request);  
  2. if(isMultipart){    //type=multipart/form-data  
  3.     DiskFileItemFactory factory = new DiskFileItemFactory();  
  4.     ServletFileUpload upload = new ServletFileUpload(factory);  
  5.     upload.setFileSizeMax(1024*1024);                       //設(shè)置上傳文件的最大容量  
  6.     List<FileItem>items  = upload.parseRequest(request);  //取得表單全部數(shù)據(jù)  
  7.     for(FileItem item:items){  
  8.         if(item.isFormField()){ //如果不是文件類(lèi)型  
  9.             //String name = item.getFieldName();    表單中某個(gè)控件的名稱(chēng)  
  10.             //String value = item.getString();      表單中某個(gè)控件的值  
  11.         }  
  12.         else{  
  13.             //String filename = item.getName(); 返回文件名稱(chēng)  
  14.              File f = null;     //保存的文件  
  15.              item.write(f);             //保存文件  
  16.         }  
  17.     }  
  18. }  
  19. else{  
  20.     //如果沒(méi)有文件上傳  
  21. }  


FileUpload是apache的commons項(xiàng)目的子項(xiàng)目,需要下載jar包,注意:還要把commons-io.jar也下下來(lái),因?yàn)檫@兩個(gè)包是相互關(guān)聯(lián)的。


一般需要導(dǎo)入:

(1)org.apache.commons.fileupload.*;

(2)org.apache.commons.fileupload.servlet.*;

(3)org.apache.commons.fileupload.disk.*;


1.一般代碼流程


boolean isMultipart = ServletFileUpload.isMultipartContent(request);//判斷表單的類(lèi)型是不是multipart/form-data

DiskFileItemFactory factory = new DiskFileItemFactory();

ServletFileUpload upload = new ServletFileUpload(factory);

upload.setFileSizeMax(1024*1024);        //設(shè)置上傳文件的最大容量

List<FileItem>items  = upload.parseRequest(request);  //取得表單全部數(shù)據(jù)

含有文件上傳控件的表單是不能區(qū)分一般控件和上傳控件的,都作為FileItem;


2.區(qū)分一般控件數(shù)據(jù)和文件上傳控件數(shù)據(jù)


通過(guò)item.isFormField()能夠判斷,如果返回true,則表示一般控件數(shù)據(jù)。


3.FileItem用法



(1)如果是一般控件,則item.getString()即可。

item.getFieldName()返回域名稱(chēng)。

(2)如果是文件上傳控件,則包含一些方法

        item.getName();    取得上傳文件的名稱(chēng)

        item.getContentType();    取得上傳文件的mime類(lèi)型

        long item.getSize();取得上傳文件的大小

        item.getInputStream();取得上傳文件的輸入流


4.保存文件


在SmartUpload中,只需要save函數(shù)即可,但是在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.對(duì)于大文件

對(duì)于打文件,需要設(shè)置臨時(shí)文件夾,形式:

factory.setRepository("filename");

本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶(hù)發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)
打開(kāi)APP,閱讀全文并永久保存 查看更多類(lèi)似文章
猜你喜歡
類(lèi)似文章
servlet 上傳文件
Java文件上傳類(lèi)FileUploadUtil.java代碼+注釋
文件上傳
《Java Web應(yīng)用程序開(kāi)發(fā)》10 Java實(shí)用技術(shù)
Java文件上傳與下載【面試+工作】
Struts 2上傳文件小談
更多類(lèi)似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長(zhǎng)圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服