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 阅读(322)
评论(0) 编辑 收藏