也談一下檔案上傳 (轉)

amyz發表於2007-08-15
也談一下檔案上傳 (轉)[@more@]

在這裡看到很多討論的文章,覺得各有利敝,有些只限於上傳檔案,而不能同時取得文字欄位值,尤其是上傳多個檔案比較少,現本人做這個上傳檔案的類最多可支援上傳255個檔案,同時可取得文字欄位值,請各位高手指正.

檔案上傳類:MoqBean.

package.moq.www; import java.io.*; import java.util.*; import javax..*; import javax.servlet.http.*; /** * *

Title: 檔案上傳類

*

Description: 既能對檔案進行上傳,又能取得輸入框的值,最多可同時上傳255個檔案

*

Copyright: Copyright (c) 2002

*

Company: Tekson

* @author 莫瓊 * @version 1.0 */ public class MoqUploaean { private String[] File = new String[255]; //原始檔名 private String[] suffix = new String[255]; //檔案字尾名 private String canSuffix = ".gif.jpg.jpeg.png"; //可上傳的檔案字尾名 private String Path = "c:/"; //目標檔案目錄 private String[] objectFileName = new String[255]; //目標檔名 private ServletInputStream sis = null; //輸入流 private String[] description = new String[255]; //描述狀態 private long size = 100 * 1024; //限制大小 private int count = 0; //已傳輸檔案數目 private byte[] b = new byte[4096]; //位元組流存放陣列 private boolean succesul = true; private Hashtable fields = new Hashtable(); public MoqUploadBean() { } //設定上傳檔案的字尾名 public void setSuffix(String canSuffix) { this.canSuffix = canSuffix; } //設定檔案儲存路徑 public void setObjectPath(String objectPath) { this.objectPath = objectPath; } //設定檔案儲存路徑 public void setSize(long maxSize) { this.size = maxSize; } //檔案上傳處理 public void setSourceFile(HttpServletRequest request) throws IOException { sis = request.getInputStream(); int a = 0; int k = 0; String s = ""; while ( (a = sis.readLine(b, 0, b.length)) != -1) { s = new String(b, 0, a); if ( (k = s.indexOf("filename="")) != -1) { // 取得檔案資料 s = s.substring(k + 10); k = s.indexOf("""); s = s.substring(0, k); sourceFile[count] = s; k = s.lastIndexOf("."); suffix[count] = s.substring(k + 1); if (canTransfer(count)) { transferFile(count); } ++count; } else if ( (k = s.indexOf("name="")) != -1) { // 普通表單輸入元素,獲取輸入元素名字 String fieldName = s.substring(k+6, s.length()-3); sis.readLine(b, 0, b.length); StringBuffer fieldValue = new StringBuffer(b.length); while ( (a = sis.readLine(b, 0, b.length)) != -1) { s = new String(b, 0, a-2); if ( (b[0] == 45) && (b[1] == 45) && (b[2] == 45) && (b[3] == 45) && (b[4] == 45)) { break; } else { fieldValue.append(s); } } fields.put(fieldName, fieldValue.toString()); } if (!successful) break; } } //取得表單元素值 public String getFieldValue(String fieldName) { if (fields == null || fieldName == null) { return null; } return (String) fields.get(fieldName); } //取得上傳檔案數 public int getCount() { return count; } //取得目標路徑 public String getObjectPath() { return objectPath; } //取得原始檔名 public String[] getSourceFile() { return sourceFile; } //取得目標檔名 public String[] getObjectFileName() { return objectFileName; } //取得上傳狀態描述 public String[] getDescription() { return description; } //判斷上傳檔案的型別 private boolean canTransfer(int i) { suffix[i] = suffix[i].toLowerCase(); //這個是用來傳圖片的,各位可以把字尾名改掉或者不要這個條件 if (sourceFile[i].equals("") || (!(canSuffix.indexOf("."+suffix[i])>=0))) { description[i] = "ERR: File suffix is wrong."; return false; } else { return true; } } //上傳檔案轉換 private void transferFile(int i) { String x = Long.toString(new java.util.Date().getTime()); try { objectFileName[i] = x + "." + suffix[i]; FileOutputStream out = new FileOutputStream(objectPath + objectFileName[i]); int a = 0; int k = 0; long hastransfered = 0; //標示已經傳輸的位元組數 String s = ""; while ( (a = sis.readLine(b, 0, b.length)) != -1) { s = new String(b, 0, a); if ( (k = s.indexOf("Content-Type:")) != -1) { break; } } sis.readLine(b, 0, b.length); while ( (a = sis.readLine(b, 0, b.length)) != -1) { s = new String(b, 0, a); if ( (b[0] == 45) && (b[1] == 45) && (b[2] == 45) && (b[3] == 45) && (b[4] == 45)) { break; } out.write(b, 0, a); hastransfered += a; if (hastransfered >= size) { description[count] = "ERR: The file " + sourceFile[count] + " is too large to transfer. The whole process is interrupted."; successful = false; break; } } if (successful) { description[count] = "Right: The file " + sourceFile[count] + " has been transfered successfully."; } out.close(); if (!successful) { sis.close(); File tmp = new File(objectPath + objectFileName[count]); tmp.delete(); } } catch (IOException ioe) { description[i] = ioe.toString(); } } public static void main(String[] args) { System.out.println("Test OK"); } }

檔案上傳:MoqUpload.

〈%@ page contentType="text/html; charset=GB2312" %> 〈html> 〈head> 〈title>檔案上載〈/title> 〈/head> 〈body> 〈foaction="MoqUploadSubmit.jsp" enctype="MULTIPART/FORM-DATA" method="post"> 作者姓名:〈input type="text" name="Author" /> 〈br /> 公司名稱:〈input type="text" name="Company" /> 〈br /> 檔案描述:〈input type="text" name="Comment" /> 〈br /> 選擇檔案1:〈input type="file" name="filename1" /> 〈br /> 選擇檔案2:〈input type="file" name="filename2" /> 〈br /> 選擇檔案3:〈input type="file" name="filename3" /> 〈br /> 選擇檔案4:〈input type="file" name="filename4" /> 〈br /> 〈input type="submit" value="上載" /> 〈/form> 〈/body> 〈/html>


檔案上傳提交:MoqUploadSubmit.jsp

〈%@ page contentType="text/html;charset=gb2312"%> 〈jsp:useBean id="fileBean" pe="page" class="net.moq." /> 〈% fileBean.setObjectPath("D:Temp"); fileBean.setSize(10000*1024); fileBean.setSuffix(".gif.jpg.png.jpge.html.htm"); fileBean.setSourceFile(request); String [] ourceFile = fileBean.getSourceFile(); String [] saObjectFile = fileBean.getObjectFileName(); String [] saDescription = fileBean.getDescription(); int iCount = fileBean.getCount(); String sObjectPath = fileBean.getObjectPath(); for(int i=0;i〈iCount;i++) { out.println("〈br>源始檔案:"); out.println(saSourceFile[i]); out.println("〈br>目標檔案:"); out.println(sObjectPath+saObjectFile[i]); out.println("〈br>上傳說明:"); out.println(saDescription[i]); out.println("〈br>"); } out.println("〈br>作者:" + fileBean.getFieldValue("Author")); out.println("〈br>公司:" + fileBean.getFieldValue("Company")); out.println("〈br>說明:" + fileBean.getFieldValue("Comment")); %>


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10752019/viewspace-957565/,如需轉載,請註明出處,否則將追究法律責任。

相關文章