1.使用fileupload實現:
所需jar包:
commons-fileupload-1.2.1.jar
commons-io-1.4.jar
用于請求上傳的頁面核心代碼:
<!-- enctype屬性為表單定義了MIME編碼方式,上傳文件的表單enctype屬性必須如此設置 -->
<form method="post" action="UploadFile" enctype="multipart/form-data" >
<p><input type="file" name="myfile1" value="瀏覽文件
" /></p>
<p><input type="submit" value="上傳"/></p>
</form>
用于上傳的Servlet核心代碼:
1 boolean isMultipart = ServletFileUpload.isMultipartContent(request);
2
3 if (isMultipart == true) {
4 try {
5 FileItemFactory factory = new DiskFileItemFactory();
6 ServletFileUpload upload = new ServletFileUpload(factory);
7
8 // 得到所有的文件
9 List<FileItem> items = upload.parseRequest(request);
10 Iterator<FileItem> itr = items.iterator();
11
12 // 依次處理每個文件
13 while (itr.hasNext()) {
14 FileItem item = (FileItem) itr.next();
15
16 // 獲得文件名及路徑
17 String fileName = item.getName();
18 if (fileName != null) {
19 File fullFile = new File(item.getName());
20
21 // UploadUtil.getUploadPath()
22 File savedFile = new File(UploadUtil.getUploadPath(),
23 fullFile.getName());
24 item.write(savedFile);
25 }
26 }
27
28 System.out.println("upload succeed");
29 } catch (Exception e) {
30 e.printStackTrace();
31 }
32 } else {
33 System.out.println("the enctype must be multipart/form-data");
34 }
35
完整工程地址:
http://other.sotee.com/2010-02-14/93617.html
2.無組件實現上傳,并且同時讀取表單
主要思想:使用request.getInputStream();獲取輸入流解析該字符串
核心代碼:
1 package com.linying.util;
2
3 import java.io.File;
4 import java.io.FileOutputStream;
5 import java.io.IOException;
6
7 import javax.servlet.ServletInputStream;
8 import javax.servlet.http.HttpServletRequest;
9
10 import com.sun.org.apache.xalan.internal.xsltc.runtime.Hashtable;
11 /**
12 * 上傳文件同時讀取表單
13 * @author Ying-er
14 * @time 2009-9-18 下午07:41:42
15 */
16 public class MyUploadBean{
17
18 private String sourceFile = null; // 源文件名
19
20 private String suffix = null; // 文件后綴名
21
22 private String canSuffix = ".gif.jpg.jpeg.png"; // 可上傳的文件后綴名
23
24 private String objectPath = UploadUtil.getUploadPath(); // 目標文件目錄
25
26 private String objectFileName = null; // 目標文件名
27
28 private ServletInputStream sis = null; // 輸入流
29
30 private String description = null; // 描述狀態
31
32 private long size = 100 * 100; // 限制大小
33
34 private byte[] b = new byte[4096]; // 字節流緩沖區
35
36 private boolean successful = true;
37
38 private Hashtable fields = new Hashtable();
39 public void setSuffix(String canSuffix) {
40 this.canSuffix = canSuffix;
41 }
42
43 // 設置文件保存路徑
44 public void setObjectPath(String objectPath) {
45 this.objectPath = objectPath;
46 }
47
48 // 設置文件保存路徑
49 public void setSize(long maxSize) {
50 this.size = maxSize;
51 }
52
53 // 文件上傳處理程序
54 public void setSourceFile(HttpServletRequest request) throws IOException {
55 sis = request.getInputStream();
56 int a = 0;
57 int k = 0;
58 String s = "";
59 while ((a = sis.readLine(b, 0, b.length)) != -1) {
60 s = new String(b, 0, a);//s = 整個請求字符串
61 if ((k = s.indexOf("filename=\"")) != -1) {
62 // 找到上傳文件的name值取得文件數據
63 s = s.substring(k + 10);//s = filename="之后的字符串
64 k = s.indexOf("\"");//k = 該節點的結束標記“/”
65 s = s.substring(0, k);//s = filename 的絕對路徑
66 sourceFile = s;//第一個文件為 s
67 k = s.lastIndexOf(".");//取得后綴“.”的索引值
68 suffix = s.substring(k + 1);//取得文件后綴
69 if (canTransfer()) {
70 transferFile();
71 }
72 }
73 else if ((k = s.indexOf("name=\"")) != -1) {
74 // 普通表單輸入元素,獲取輸入元素名字
75 String fieldName = s.substring(k + 6, s.length() - 3);//name="——6個," /——3個 所以能夠獲取到name的值
76 //sis.readLine(b, 0, b.length);
77 StringBuffer fieldValue = new StringBuffer(b.length);
78 while ((a = sis.readLine(b, 0, b.length)) != -1) {
79 s = new String(b, 0, a - 2);
80 if ((b[0] == 45) && (b[1] == 45) && (b[2] == 45)
81 && (b[3] == 45) && (b[4] == 45)) {
82 break;
83 } else {
84 fieldValue.append(s);
85 }
86 }
87 fields.put(fieldName, fieldValue.toString());
88 }
89 if (!successful)
90 break;
91 }
92 }
93
94 // 取得表單元素值
95 public String getFieldValue(String fieldName) {
96 if (fields == null || fieldName == null) {
97 return null;
98 }
99 System.out.println(fields.get("Author"));
100 return (String) fields.get(fieldName);
101 }
102
103
104 // 取得目標路徑
105 public String getObjectPath() {
106 return objectPath;
107 }
108
109 // 取得源文件名
110 public String getSourceFile() {
111 return sourceFile;
112 }
113
114 // 取得目標文件名
115
116 public String getObjectFileName() {
117 return objectFileName;
118 }
119
120 // 取得上傳狀態描述
121
122 public String getDescription() {
123 return description;
124 }
125
126 // 判斷上傳文件的類型
127
128 private boolean canTransfer() {
129 suffix = suffix.toLowerCase();
130 // 這個是用來傳圖片的,各位可以把后綴名改掉或者不要這個條件
131 if (sourceFile.equals("")
132 || (!(canSuffix.indexOf("." + suffix) >= 0))) {
133 description = "ERR: File suffix is wrong.";
134 return false;
135 }
136 else {
137 return true;
138 }
139 }
140
141 // 上傳文件轉換
142 private void transferFile() {
143 String x = Long.toString(new java.util.Date().getTime());
144 try {
145 objectFileName = x + "." + suffix;
146 FileOutputStream out = new FileOutputStream(objectPath
147 + objectFileName);
148 System.out.println(objectPath+objectFileName);
149 int a = 0;
150 int k = 0;
151 long hastransfered = 0; // 標示已經傳輸的字節數
152 String s = "";
153 while ((a = sis.readLine(b, 0, b.length)) != -1) {
154 s = new String(b, 0, a);
155 if ((k = s.indexOf("Content-Type:")) != -1) {
156 break;
157 }
158 }
159 sis.readLine(b, 0, b.length);
160 while ((a = sis.readLine(b, 0, b.length)) != -1) {
161 s = new String(b, 0, a);
162 if ((b[0] == 45) && (b[1] == 45) && (b[2] == 45)
163 && (b[3] == 45) && (b[4] == 45)) {
164 break;
165 }
166 out.write(b, 0, a);
167 hastransfered += a;
168 if (hastransfered >= size) {
169 description = "ERR: This file is too large to transfer(100*100). The whole process is interrupted.";
170 successful = false;
171 break;
172 }
173 }
174
175 if (successful) {
176 description = "Right: The file " + sourceFile +
177 " has been transfered successfully.";
178 }
179 out.close();
180 if (!successful) {
181 sis.close();
182 File tmp = new File(objectPath + objectFileName);
183 tmp.delete();
184 }
185 }
186 catch (IOException ioe) {
187 description = ioe.toString();
188 }
189 }
190
191
192 }
完整工程地址:
http://other.sotee.com/2010-02-17/93855.html
相同思想的演變版本(使用Servlet實現)
完整工程地址:
http://other.sotee.com/2010-02-17/93856.html
posted on 2010-02-26 19:55
Ying-er 閱讀(328)
評論(0) 編輯 收藏