|
@ -1,19 +1,26 @@
|
1
|
1
|
package com.ai.server.bean;
|
2
|
2
|
|
|
3
|
import java.io.BufferedInputStream;
|
|
4
|
import java.io.BufferedOutputStream;
|
3
|
5
|
import java.io.File;
|
4
|
6
|
import java.io.FileInputStream;
|
5
|
7
|
import java.io.FileOutputStream;
|
6
|
8
|
import java.io.InputStream;
|
7
|
9
|
import java.io.OutputStream;
|
|
10
|
import java.text.SimpleDateFormat;
|
|
11
|
import java.util.Date;
|
|
12
|
import java.util.Iterator;
|
8
|
13
|
import java.util.List;
|
9
|
14
|
|
10
|
15
|
import javax.servlet.http.HttpServletRequest;
|
11
|
16
|
import javax.servlet.http.HttpServletResponse;
|
12
|
17
|
|
13
|
18
|
import org.apache.commons.fileupload.FileItem;
|
14
|
|
import org.apache.commons.fileupload.FileUploadException;
|
|
19
|
import org.apache.commons.fileupload.FileItemFactory;
|
15
|
20
|
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
|
16
|
21
|
import org.apache.commons.fileupload.servlet.ServletFileUpload;
|
|
22
|
import org.apache.commons.fileupload.util.Streams;
|
|
23
|
import org.apache.commons.lang.StringUtils;
|
17
|
24
|
|
18
|
25
|
import com.ai.server.core.bean.DisplayBean;
|
19
|
26
|
import com.ai.server.util.ApplicationPath;
|
|
@ -29,6 +36,14 @@ import com.ailk.mobile.util.MobileUtility;
|
29
|
36
|
*
|
30
|
37
|
*/
|
31
|
38
|
public class UploadDownloadBean extends DisplayBean {
|
|
39
|
|
|
40
|
/**
|
|
41
|
* @Fields hasTimeRandom : 上传图片的路径中,是否包含时间串儿,例如:201511022351123_1234
|
|
42
|
* 如果为true,则可以保证每次上传的图片不重名,可以保留原文件的名称不变,下载时候可获取原文件的名称。
|
|
43
|
*
|
|
44
|
*/
|
|
45
|
private final boolean hasTimeRandom = false;
|
|
46
|
|
32
|
47
|
/************************************************************************************
|
33
|
48
|
* 文件上传;
|
34
|
49
|
* 如果在参数param中包含USER_NAME键值对,那么文件将保存到USER_NAME对应的目录下;否则保存在TEMP目录下
|
|
@ -37,7 +52,7 @@ public class UploadDownloadBean extends DisplayBean {
|
37
|
52
|
* @return
|
38
|
53
|
* @throws Exception
|
39
|
54
|
*/
|
40
|
|
public IData upload(IData param) throws Exception{
|
|
55
|
/*public IData upload(IData param) throws Exception{
|
41
|
56
|
IData result = new DataMap();
|
42
|
57
|
HttpServletRequest request = ServletManager.getRequest();
|
43
|
58
|
if (!ServletFileUpload.isMultipartContent(request)) {
|
|
@ -119,7 +134,7 @@ public class UploadDownloadBean extends DisplayBean {
|
119
|
134
|
MobileUtility.error("上传失败", e);
|
120
|
135
|
}
|
121
|
136
|
return result;
|
122
|
|
}
|
|
137
|
}*/
|
123
|
138
|
/************************************************************************************
|
124
|
139
|
* 文件下载;
|
125
|
140
|
*
|
|
@ -161,4 +176,130 @@ public class UploadDownloadBean extends DisplayBean {
|
161
|
176
|
|
162
|
177
|
return null;
|
163
|
178
|
}
|
|
179
|
|
|
180
|
public IData upload(IData param) throws Exception{
|
|
181
|
IData returnMap = new DataMap(); // 返回的对象
|
|
182
|
HttpServletRequest request = ServletManager.getRequest();
|
|
183
|
HttpServletResponse response = ServletManager.getResponse();
|
|
184
|
response.setContentType("text/html;charset=UTF-8");
|
|
185
|
response.setHeader("Cache-Control", "no-cache");
|
|
186
|
if (!ServletFileUpload.isMultipartContent(request)) {
|
|
187
|
MobileUtility.error("获取的上传文件并非以文件流格式传送,请核查!");
|
|
188
|
}
|
|
189
|
|
|
190
|
log.debug("【上传资源开始。。。】");
|
|
191
|
Date date = new Date();// 获取当前时间
|
|
192
|
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmssSSS");
|
|
193
|
String dateStr = formatter.format(date);
|
|
194
|
int random = (int)(Math.random()*10000);
|
|
195
|
String dateStr_random = dateStr+"_"+random; //时间字符串+随机数 最后一级别的文件夹
|
|
196
|
String path = null; // 存放数据库用 相对路径 path = typePath + dateStr_random + fileName
|
|
197
|
String rootDir = ApplicationPath.getFilePath(request).replace("//", File.separator).replace("\\", File.separator) + File.separator + "upload"; // eg:F:\eclipseWorkspace3\android-share\display-server\web
|
|
198
|
String typePath = null;
|
|
199
|
String filePath = null; // 上传服务器用 绝对路径 filePath = rootDir + typePath + dateStr_random + fileName
|
|
200
|
String fileName = null;
|
|
201
|
BufferedInputStream in = null;
|
|
202
|
BufferedOutputStream outStream = null;
|
|
203
|
try {
|
|
204
|
// 获得磁盘文件条目工厂
|
|
205
|
FileItemFactory factory = new DiskFileItemFactory();
|
|
206
|
// 高水平的API文件上传处理
|
|
207
|
ServletFileUpload upload = new ServletFileUpload(factory);
|
|
208
|
upload.setHeaderEncoding("UTF-8");
|
|
209
|
// 获取多个上传文件
|
|
210
|
List<?> fileList = upload.parseRequest(request);
|
|
211
|
// 遍历上传文件写入磁盘
|
|
212
|
Iterator<?> it = fileList.iterator();
|
|
213
|
|
|
214
|
while (it.hasNext()) {
|
|
215
|
FileItem item = (FileItem) it.next();
|
|
216
|
if (item == null) {
|
|
217
|
continue;
|
|
218
|
}
|
|
219
|
//获取表单的属性名字
|
|
220
|
String name = item.getFieldName();
|
|
221
|
//如果获取的表单信息,是普通的文本信息
|
|
222
|
if (item.isFormField()) {
|
|
223
|
//获取用户具体输入的字符串,因为表单提交过来的是字符串类型的
|
|
224
|
String value = item.getString();
|
|
225
|
if ("data".equals(name)) {
|
|
226
|
IData postData = new DataMap(value);
|
|
227
|
typePath = postData.getString("UPLOAD_PATH", "temp");
|
|
228
|
fileName = postData.getString("FILE_NAME");
|
|
229
|
}
|
|
230
|
request.setAttribute(name, value);
|
|
231
|
continue;
|
|
232
|
}
|
|
233
|
|
|
234
|
// 获取路径名
|
|
235
|
String value = item.getName();
|
|
236
|
if (value != null) {
|
|
237
|
// 索引到最后一个反斜杠
|
|
238
|
int start = value.lastIndexOf("\\");
|
|
239
|
// 截取 上传文件的 字符串名字,加1是 去掉反斜杠,
|
|
240
|
String originalFileName = value.substring(start + 1); // 文件名称
|
|
241
|
if (StringUtils.isEmpty(fileName)) {
|
|
242
|
fileName = originalFileName;
|
|
243
|
}
|
|
244
|
|
|
245
|
String mulu = null;
|
|
246
|
if (hasTimeRandom) {
|
|
247
|
path = FileUtil.connectFilePath(typePath, dateStr_random, fileName);
|
|
248
|
mulu = FileUtil.connectFilePath(rootDir, typePath, dateStr_random);
|
|
249
|
}
|
|
250
|
else {
|
|
251
|
path = FileUtil.connectFilePath(typePath, fileName);
|
|
252
|
mulu = FileUtil.connectFilePath(rootDir, typePath);
|
|
253
|
}
|
|
254
|
filePath = FileUtil.connectFilePath(rootDir, path);
|
|
255
|
File file = new File(mulu);
|
|
256
|
if (!file.isDirectory()) {
|
|
257
|
log.debug("目录不存在,自动生成目录:" + mulu);
|
|
258
|
file.mkdirs();
|
|
259
|
}
|
|
260
|
in = new BufferedInputStream(item.getInputStream());// 获得文件输入流
|
|
261
|
outStream = new BufferedOutputStream(new FileOutputStream(new File(filePath)));// 获得文件输出流
|
|
262
|
long filelenth = Streams.copy(in, outStream, true);// 开始把文件写到你指定的上传文件夹
|
|
263
|
|
|
264
|
returnMap.put("size", filelenth);
|
|
265
|
returnMap.put("lujing", path);
|
|
266
|
returnMap.put("fileName", fileName);
|
|
267
|
|
|
268
|
}
|
|
269
|
}
|
|
270
|
log.debug("【上传结束:】" + returnMap.toString());
|
|
271
|
} catch (Exception e) {
|
|
272
|
// TODO Auto-generated catch block
|
|
273
|
e.printStackTrace();
|
|
274
|
}
|
|
275
|
finally
|
|
276
|
{
|
|
277
|
if (in != null)
|
|
278
|
{
|
|
279
|
try
|
|
280
|
{
|
|
281
|
in.close();
|
|
282
|
}
|
|
283
|
catch (Exception e)
|
|
284
|
{
|
|
285
|
throw new RuntimeException(e);
|
|
286
|
}
|
|
287
|
|
|
288
|
}
|
|
289
|
if (outStream != null)
|
|
290
|
{
|
|
291
|
try
|
|
292
|
{
|
|
293
|
outStream.close();
|
|
294
|
}
|
|
295
|
catch (Exception e)
|
|
296
|
{
|
|
297
|
throw new RuntimeException(e);
|
|
298
|
}
|
|
299
|
|
|
300
|
}
|
|
301
|
}
|
|
302
|
|
|
303
|
return returnMap;
|
|
304
|
}
|
164
|
305
|
}
|