|
@ -1,119 +1,120 @@
|
1
|
|
package com.ai.server.bean;
|
2
|
|
|
3
|
|
import java.io.File;
|
4
|
|
import java.io.FileInputStream;
|
5
|
|
import java.io.FileOutputStream;
|
6
|
|
import java.io.InputStream;
|
7
|
|
import java.io.OutputStream;
|
8
|
|
import java.util.Iterator;
|
9
|
|
import java.util.List;
|
10
|
|
|
11
|
|
import javax.servlet.http.HttpServletRequest;
|
12
|
|
|
13
|
|
import org.apache.commons.fileupload.FileItem;
|
14
|
|
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
|
15
|
|
import org.apache.commons.fileupload.servlet.ServletFileUpload;
|
16
|
|
|
17
|
|
import com.ai.ipu.server.servlet.ServletManager;
|
18
|
|
import com.ai.ipu.server.util.MobileUtility;
|
19
|
|
import com.ai.server.core.bean.IpuAppBean;
|
20
|
|
import com.ai.server.util.ApplicationPath;
|
21
|
|
import com.ailk.common.data.IData;
|
22
|
|
import com.ailk.common.data.impl.DataMap;
|
23
|
|
|
24
|
|
/**
|
25
|
|
* @author huangbo
|
26
|
|
* @date 2016-1-9 下午2:32:20
|
27
|
|
* @desc 上传下载使用范例
|
28
|
|
*/
|
29
|
|
public class UploadDownloadBean extends IpuAppBean {
|
30
|
|
|
31
|
|
/**
|
32
|
|
* 文件上传
|
33
|
|
*/
|
34
|
|
public IData upload(IData param) throws Exception{
|
35
|
|
/*1.判断请求和参数是否合法*/
|
36
|
|
String filePathStr = param.getString("FILE_PATH", ""); //通过自定义参数决定文件存储规则
|
37
|
|
String[] filePaths = filePathStr.split(",");
|
38
|
|
if(filePaths.length == 0){
|
39
|
|
MobileUtility.error("请设置文件存储路径!");
|
40
|
|
}
|
41
|
|
/*2.判断请求是否包含文件信息*/
|
42
|
|
HttpServletRequest request = ServletManager.getRequest();
|
43
|
|
if (!ServletFileUpload.isMultipartContent(request)) {
|
44
|
|
MobileUtility.error("没有检测到文件,请重新提交!");
|
45
|
|
}
|
46
|
|
|
47
|
|
/*3.处理上传文件的业务逻辑*/
|
48
|
|
IData result = new DataMap();
|
49
|
|
DiskFileItemFactory factory = new DiskFileItemFactory(); //获得磁盘文件条目工厂
|
50
|
|
//factory.setSizeThreshold(5*1024*1024); //设置缓存的大小。上传文件超过缓存时,将存储在临时目录中。
|
51
|
|
//factory.setRepository(file); //设置临时存储
|
52
|
|
ServletFileUpload upload = new ServletFileUpload(factory);
|
53
|
|
OutputStream out = null;
|
54
|
|
InputStream in = null;
|
55
|
|
String filePath = null, savePath = ""; //文件地址
|
56
|
|
try {
|
57
|
|
/*多个上传文件*/
|
58
|
|
List<FileItem> fileList = upload.parseRequest(request);
|
59
|
|
|
60
|
|
/*fileList之中包含了表单参数*/
|
61
|
|
Iterator<FileItem> it = fileList.iterator();
|
62
|
|
int i=0;
|
63
|
|
while(it.hasNext()){
|
64
|
|
FileItem item = it.next();
|
65
|
|
String name = item.getFieldName();// 获取表单的属性名
|
66
|
|
|
67
|
|
/*处理表单数据*/
|
68
|
|
if (item.isFormField()) {
|
69
|
|
request.setAttribute(name, item.getString());
|
70
|
|
} else {
|
71
|
|
String value = item.getName();
|
72
|
|
request.setAttribute(name, value); // 保存文件名
|
73
|
|
/*文件在server上保存地址*/
|
74
|
|
filePath = ApplicationPath.getFilePath(request) + filePaths[i];
|
75
|
|
File dir = new File(filePath).getParentFile();
|
76
|
|
if (!dir.exists() && !dir.mkdirs()) {
|
77
|
|
MobileUtility.error("创建上传文件夹失败!");
|
78
|
|
}
|
79
|
|
savePath += filePath + ",";
|
80
|
|
/* 保存文件 */
|
81
|
|
File uploadFile = new File(filePath);
|
82
|
|
out = new FileOutputStream(uploadFile);
|
83
|
|
in = item.getInputStream();
|
84
|
|
int length = 0;
|
85
|
|
byte[] buf = new byte[1024];
|
86
|
|
while ((length = in.read(buf)) != -1) {
|
87
|
|
out.write(buf, 0, length);
|
88
|
|
}
|
89
|
|
i++;
|
90
|
|
log.debug("上传文件大小为:" + item.getSize());
|
91
|
|
}
|
92
|
|
}
|
93
|
|
|
94
|
|
} catch (Exception e) {
|
95
|
|
MobileUtility.error("上传文件失败!", e);
|
96
|
|
} finally {
|
97
|
|
if (in != null)
|
98
|
|
in.close();
|
99
|
|
if (out != null)
|
100
|
|
out.close();
|
101
|
|
}
|
102
|
|
result.put("FILE_PATH", savePath.substring(0, savePath.length() - 1));
|
103
|
|
result.put("MEESSAG", "上传文件成功:" + filePath);
|
104
|
|
return result;
|
105
|
|
}
|
106
|
|
|
107
|
|
public InputStream download(IData param) throws Exception {
|
108
|
|
String filePath = param.getString("FILE_PATH");
|
109
|
|
HttpServletRequest request = ServletManager.getRequest();
|
110
|
|
filePath = ApplicationPath.getFilePath(request) + filePath;
|
111
|
|
|
112
|
|
File file = new File(filePath);
|
113
|
|
if (!file.isFile()) {
|
114
|
|
MobileUtility.error("文件不存在:" + filePath);
|
115
|
|
}
|
116
|
|
|
117
|
|
return new FileInputStream(file);
|
118
|
|
}
|
119
|
|
}
|
|
1
|
package com.ai.server.bean;
|
|
2
|
|
|
3
|
import java.io.File;
|
|
4
|
import java.io.FileInputStream;
|
|
5
|
import java.io.FileOutputStream;
|
|
6
|
import java.io.InputStream;
|
|
7
|
import java.io.OutputStream;
|
|
8
|
import java.util.Iterator;
|
|
9
|
import java.util.List;
|
|
10
|
|
|
11
|
import javax.servlet.http.HttpServletRequest;
|
|
12
|
|
|
13
|
import org.apache.commons.fileupload.FileItem;
|
|
14
|
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
|
|
15
|
import org.apache.commons.fileupload.servlet.ServletFileUpload;
|
|
16
|
|
|
17
|
import com.ai.ipu.server.servlet.ServletManager;
|
|
18
|
import com.ai.ipu.server.util.MobileUtility;
|
|
19
|
import com.ai.server.core.bean.IpuAppBean;
|
|
20
|
import com.ai.server.util.ApplicationPath;
|
|
21
|
import com.ailk.common.data.IData;
|
|
22
|
import com.ailk.common.data.impl.DataMap;
|
|
23
|
|
|
24
|
/**
|
|
25
|
* @author huangbo
|
|
26
|
* @date 2016-1-9 下午2:32:20
|
|
27
|
* @desc 上传下载使用范例
|
|
28
|
*/
|
|
29
|
public class UploadDownloadBean extends IpuAppBean {
|
|
30
|
|
|
31
|
/**
|
|
32
|
* 文件上传
|
|
33
|
*/
|
|
34
|
public IData upload(IData param) throws Exception {
|
|
35
|
/* 1.判断请求和参数是否合法 */
|
|
36
|
String filePathStr = param.getString("FILE_PATH", ""); // 通过自定义参数决定文件存储规则
|
|
37
|
String[] filePaths = filePathStr.split(",");
|
|
38
|
if (filePaths.length == 0) {
|
|
39
|
MobileUtility.error("请设置文件存储路径!");
|
|
40
|
}
|
|
41
|
/* 2.判断请求是否包含文件信息 */
|
|
42
|
HttpServletRequest request = ServletManager.getRequest();
|
|
43
|
if (!ServletFileUpload.isMultipartContent(request)) {
|
|
44
|
MobileUtility.error("没有检测到文件,请重新提交!");
|
|
45
|
}
|
|
46
|
|
|
47
|
/* 3.处理上传文件的业务逻辑 */
|
|
48
|
IData result = new DataMap();
|
|
49
|
DiskFileItemFactory factory = new DiskFileItemFactory(); // 获得磁盘文件条目工厂
|
|
50
|
// factory.setSizeThreshold(5*1024*1024); //设置缓存的大小。上传文件超过缓存时,将存储在临时目录中。
|
|
51
|
// factory.setRepository(file); //设置临时存储
|
|
52
|
ServletFileUpload upload = new ServletFileUpload(factory);
|
|
53
|
OutputStream out = null;
|
|
54
|
InputStream in = null;
|
|
55
|
String filePath = null, savePath = ""; // 文件地址
|
|
56
|
|
|
57
|
/* 多个上传文件 */
|
|
58
|
List<FileItem> fileList = upload.parseRequest(request);
|
|
59
|
|
|
60
|
/* fileList之中包含了表单参数 */
|
|
61
|
Iterator<FileItem> it = fileList.iterator();
|
|
62
|
int i = 0;
|
|
63
|
while (it.hasNext()) {
|
|
64
|
FileItem item = it.next();
|
|
65
|
String name = item.getFieldName();// 获取表单的属性名
|
|
66
|
|
|
67
|
/* 处理表单数据 */
|
|
68
|
if (item.isFormField()) {
|
|
69
|
request.setAttribute(name, item.getString());
|
|
70
|
} else {
|
|
71
|
String value = item.getName();
|
|
72
|
request.setAttribute(name, value); // 保存文件名
|
|
73
|
/* 文件在server上保存地址 */
|
|
74
|
filePath = ApplicationPath.getFilePath(request) + filePaths[i];
|
|
75
|
File dir = new File(filePath).getParentFile();
|
|
76
|
if (!dir.exists() && !dir.mkdirs()) {
|
|
77
|
MobileUtility.error("创建上传文件夹失败!");
|
|
78
|
}
|
|
79
|
savePath += filePath + ",";
|
|
80
|
/* 保存文件 */
|
|
81
|
File uploadFile = new File(filePath);
|
|
82
|
try {
|
|
83
|
out = new FileOutputStream(uploadFile);
|
|
84
|
in = item.getInputStream();
|
|
85
|
int length = 0;
|
|
86
|
byte[] buf = new byte[1024];
|
|
87
|
while ((length = in.read(buf)) != -1) {
|
|
88
|
out.write(buf, 0, length);
|
|
89
|
}
|
|
90
|
i++;
|
|
91
|
log.debug("上传文件大小为:" + item.getSize());
|
|
92
|
} catch (Exception e) {
|
|
93
|
MobileUtility.error("上传文件失败!", e);
|
|
94
|
} finally {
|
|
95
|
if (in != null)
|
|
96
|
in.close();
|
|
97
|
if (out != null)
|
|
98
|
out.close();
|
|
99
|
}
|
|
100
|
}
|
|
101
|
|
|
102
|
}
|
|
103
|
result.put("FILE_PATH", savePath.substring(0, savePath.length() - 1));
|
|
104
|
result.put("MEESSAG", "上传文件成功:" + filePath);
|
|
105
|
return result;
|
|
106
|
}
|
|
107
|
|
|
108
|
public InputStream download(IData param) throws Exception {
|
|
109
|
String filePath = param.getString("FILE_PATH");
|
|
110
|
HttpServletRequest request = ServletManager.getRequest();
|
|
111
|
filePath = ApplicationPath.getFilePath(request) + filePath;
|
|
112
|
|
|
113
|
File file = new File(filePath);
|
|
114
|
if (!file.isFile()) {
|
|
115
|
MobileUtility.error("文件不存在:" + filePath);
|
|
116
|
}
|
|
117
|
|
|
118
|
return new FileInputStream(file);
|
|
119
|
}
|
|
120
|
}
|