转自http://blog.csdn.net/hudou_k/archive/2005/10/17/507960.aspx
1package linkman.helper;
2
3import java.io.*;
4import java.util.*;
5import javax.servlet.http.HttpServletRequest;
6import javax.servlet.http.HttpServletResponse;
7import javax.servlet.ServletInputStream;
8
9public class UpLoadBean {
10 private String SavePath = null;
11 private String FileName = null;
12 private String Sperator = null;
13 private String ErrMsg = "正常返回";
14
15 private byte[] bt = new byte[4096];
16 private byte[] prebt = new byte[4096];
17 private int t = -1;
18 private long FileSize = -1;
19 private boolean UserDefineFileName = false;
20
21 /////////////////////////////////////////////////
22 public UpLoadBean () {
23 FileName = bringFileName ();
24 }
25 public UpLoadBean (String savepath) {
26 SavePath = savepath;
27 FileName = bringFileName ();
28 }
29
30 /////////////////////////////////////////////////
31 // 文件名信息
32 private String bringFileName () {
33 Calendar dt = Calendar.getInstance ();
34 String str = "" + dt.get (Calendar.YEAR) + dt.get (Calendar.MONTH) + dt.get (Calendar.DAY_OF_MONTH);
35 str = str + dt.get (Calendar.HOUR) + dt.get (Calendar.MINUTE) + dt.get (Calendar.SECOND);
36 return str;
37 }
38
39 public void setFileName (String filename) {
40 FileName = filename;
41 UserDefineFileName = true;
42 }
43 public String getFileName () {
44 return FileName;
45 }
46 /////////////////////////////////////////////////
47 // 上传文件主程序
48 public boolean UpLoad (HttpServletRequest request) throws Exception {
49 if (SavePath == null) {
50 setErrorMessage ("未设置保存上传文件的路径");
51 return false;
52 }
53 request.setCharacterEncoding("utf-8");
54 ServletInputStream in = request.getInputStream ();
55 ////////////////////////////////////////////
56 // 获取得一行的分隔标志
57 t = in.readLine (bt, 0, bt.length);
58 if (t != -1) {
59 Sperator = new String (bt, 0, t);
60 Sperator = Sperator.substring (0, 28);
61 t = -1;
62 }
63
64 ////////////////////////////////////////////
65 // 取扩展名并设定文件全名
66 do {
67 t = in.readLine (bt, 0, bt.length);
68 String s = new String (bt, 0, t);
69 int index = s.indexOf ("filename=\"");
70 if (index != -1) {
71 s = s.substring (index + 10);
72 index = s.indexOf ("\"");
73 s = s.substring (0, index);
74 index = s.lastIndexOf (".");
75 if (!UserDefineFileName) {
76 FileName = FileName + s.substring (index); // 设定文件全名
77 }
78 t = -1;
79 }
80 } while (t != -1);
81
82 ////////////////////////////////////////////
83 // 取文件内容
84 FileOutputStream out = new FileOutputStream (SavePath + "\\" + FileName);
85
86 t = in.readLine (bt, 0, bt.length);
87 String s = new String (bt, 0, t);
88 int i = s.indexOf ("Content-Type:");
89 if (i == -1) {
90 setErrorMessage ("上传的不是文件");
91 return false;
92 } else {
93 in.readLine (bt, 0, bt.length); // 去掉一个空行
94 t = -1;
95 }
96
97 boolean state = true;
98 long trancsize = 0;
99 try {
100 t = in.readLine (bt, 0, bt.length);
101 while (t != -1) {
102 s = new String (bt, 0, t);
103 if (s.length () > 28) {
104 s = s.substring (0, 28);
105 if (s.equals (Sperator)) {
106 break;
107 }
108 }
109
110 /**//*
111 if (bt[0] == 45 && bt[1] == 45 && bt[2] == 45 && bt[3] == 45 && bt[4] == 45) {
112 break;
113 }
114 */
115 if (FileSize != -1) {
116 if (trancsize >= FileSize) {
117 throw new Exception ("上传文件太大");
118 }
119 }
120 out.write (bt, 0, t);
121 trancsize += t;
122 t = in.readLine (bt, 0, bt.length);
123 }
124 } catch (Exception e) {
125 state = false;
126 out.close ();
127 setErrorMessage (e.getMessage ());
128 File tmp = new File (SavePath, FileName);
129 if (!tmp.delete()) {
130 setErrorMessage (e.getMessage () + ",清除未完成");
131 }
132 } finally {
133 if (state) {
134 out.close ();
135 }
136 return state;
137 }
138 }
139 /////////////////////////////////////////////////
140 // 上传文件错误信息
141 public void setErrorMessage (String errormessage) {
142 ErrMsg = errormessage;
143 }
144 public String getErrorMessage () {
145 return ErrMsg;
146 }
147 /////////////////////////////////////////////////
148 // 文件大小信息
149 public void setFileSize (long size) {
150 FileSize = size;
151 }
152
153 public long getFileSize () {
154 return FileSize;
155 }
156}// end class
157
158
posted on 2005-10-18 21:43
MudFish 阅读(288)
评论(0) 编辑 收藏 所属分类:
JSP