upfile.java
4.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package com.servlet.bksh.DMC;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
public class upfile extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 2325826340972403064L;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/html");
String type = null;
DiskFileItemFactory difactory = new DiskFileItemFactory();
// 创建文件处理工厂,它用于生成FileItem对象。
difactory.setSizeThreshold(1024 * 1024);
// 设置处理工厂缓存的临时目录,此目录下的文件需要手动删除。
// String dir = this.getServletContext().getRealPath("/");
String dir = "/usr/bk_cx/";
//String dir = "C://";
File filedir = new File(dir + "emp");
if (!filedir.exists())
filedir.mkdir();
difactory.setRepository(filedir);
// 设置文件实际保存的目录
// String userdir = dir + "bksh_info";
//
// File fudir = new File(userdir);
//
// if (!fudir.exists())
//
// fudir.mkdir();
// 创建request的解析器,它会将数据封装到FileItem对象中。
ServletFileUpload sfu = new ServletFileUpload(difactory);
// 解析保存在request中的数据并返回list集合
List<?> list = null;
try {
list = sfu.parseRequest(request);
} catch (FileUploadException e) {
e.printStackTrace();
}
// 遍历list集合,取出每一个输入项的FileItem对象,并分别获取数据
for (Iterator<?> it = list.iterator(); it.hasNext();) {
FileItem fi = (FileItem) it.next();
if (fi.isFormField()) {
//
// System.out.println("type?" + fi.getFieldName());
//
// System.out.println("value?" + fi.getString());
type = fi.getString();
} else {
// 由于客户端向服务器发送的文件是客户端的全路径,在这我们只需要文件名即可
String filename = fi.getName();
if (!filename.endsWith("jpg") && !filename.endsWith("jpeg")
&& !filename.endsWith("docx")
&& !filename.endsWith("doc")
&& !filename.endsWith("ppt")
&& !filename.endsWith("pdf")
&& !filename.endsWith(".xls")
&& filename.endsWith(".xlsx")) {
response.getWriter()
.print("{success:true,msg:'◇上传功能仅支持.jpg/.jpeg/.docx/.doc/.pdf/.xls/.xlsx格式的文件上传◇'}");
response.getWriter().close();
return;
}
int index = filename.lastIndexOf("\\");
if (index != -1)
filename = filename.substring(index + 1);
// 向服务器写出文件
InputStream in = fi.getInputStream();
String userdir = null;
if (type.equals("info"))
userdir = dir + "bksh_info";
else if (type.equals("report"))
userdir = dir + "bksh_report";
else if (type.equals("scheme"))
userdir = dir + "bksh_scheme";
File fudir = new File(userdir);
if (!fudir.exists())
fudir.mkdir();
File fudirpath = new File(fudir + "/" +filename);//"/" + DataBase.encodeFileName(request, filename)
if (fudirpath.exists()) {
response.getWriter().print(
"{success:true,msg:'◇您上传的文件已存在,请删除后再次上传◇'}");
response.getWriter().close();
return;
}
FileOutputStream fos = new FileOutputStream(fudirpath);
byte[] buf = new byte[1024];
int len = -1;
while ((len = in.read(buf)) != -1) {
fos.write(buf, 0, len);
}
// 关闭流
if (in != null) {
try {
in.close();
} finally {
if (fos != null)
fos.close();
}
}
}
}
response.getWriter().print("{success:true,msg:'◇上传成功◇'}");
response.getWriter().close();
}
}