|
@ -1,165 +0,0 @@
|
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.List;
|
9
|
|
|
10
|
|
import javax.servlet.http.HttpServletRequest;
|
11
|
|
import javax.servlet.http.HttpServletResponse;
|
12
|
|
|
13
|
|
import org.apache.commons.fileupload.FileItem;
|
14
|
|
import org.apache.commons.fileupload.FileUploadException;
|
15
|
|
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
|
16
|
|
import org.apache.commons.fileupload.servlet.ServletFileUpload;
|
17
|
|
|
18
|
|
import com.ai.server.core.bean.DisplayBean;
|
19
|
|
import com.ai.server.util.ApplicationPath;
|
20
|
|
import com.ailk.common.data.IData;
|
21
|
|
import com.ailk.common.data.impl.DataMap;
|
22
|
|
import com.ailk.mobile.servlet.ServletManager;
|
23
|
|
|
24
|
|
/**
|
25
|
|
*
|
26
|
|
* @author Lu
|
27
|
|
*
|
28
|
|
*/
|
29
|
|
public class FileUpDownload extends DisplayBean {
|
30
|
|
/************************************************************************************
|
31
|
|
* 文件上传;
|
32
|
|
* 如果在参数param中包含USER_NAME键值对,那么文件将保存到USER_NAME对应的目录下;否则保存在TEMP目录下
|
33
|
|
*
|
34
|
|
* @param param
|
35
|
|
* @return
|
36
|
|
* @throws Exception
|
37
|
|
*/
|
38
|
|
public IData upload(IData param) throws Exception{
|
39
|
|
IData result = new DataMap();
|
40
|
|
HttpServletRequest request = ServletManager.getRequest();
|
41
|
|
request.setCharacterEncoding("UTF-8");
|
42
|
|
//获取文件需要上传到的路径
|
43
|
|
String path = ApplicationPath.getFilePath(request) + "upload" + File.separator;
|
44
|
|
//获取临时目录,文件保存目录
|
45
|
|
String tempPath = path + "temp";
|
46
|
|
String savePath = null;
|
47
|
|
|
48
|
|
File file = new File(tempPath);
|
49
|
|
if(! file.exists() && ! file.mkdirs()){
|
50
|
|
throw new RuntimeException("生成上传临时文件夹失败,无法进行上传操作!");
|
51
|
|
}
|
52
|
|
//获得磁盘文件条目工厂
|
53
|
|
DiskFileItemFactory factory = new DiskFileItemFactory();
|
54
|
|
//设置暂时存放的存储室,这个存储室可以和最终存储文件的目录不同
|
55
|
|
factory.setRepository(file);
|
56
|
|
//设置缓存的大小,当上传文件的容量超过该缓存时,将产生临时文件并存储于临时目录中.
|
57
|
|
factory.setSizeThreshold(5*1024*1024);
|
58
|
|
//文件上传处理
|
59
|
|
ServletFileUpload upload = new ServletFileUpload(factory);
|
60
|
|
|
61
|
|
try{
|
62
|
|
if (! ServletFileUpload.isMultipartContent(request)) {
|
63
|
|
log.debug("获取的上传文件并非以文件流格式传送,请核查!");
|
64
|
|
return null;
|
65
|
|
}
|
66
|
|
List<?> fileList = upload.parseRequest(request);
|
67
|
|
|
68
|
|
for(int i = 0; i < fileList.size(); i++){
|
69
|
|
FileItem item = (FileItem)fileList.get(i);
|
70
|
|
//获取表单的属性名字
|
71
|
|
String name = item.getFieldName();
|
72
|
|
|
73
|
|
//如果获取的 表单信息是普通的文本信息
|
74
|
|
if(item.isFormField()){
|
75
|
|
//获取用户具体输入的字符串,因为表单提交过来的是字符串类型的
|
76
|
|
String value = item.getString();
|
77
|
|
request.setAttribute(name, value);
|
78
|
|
//获取用户保存目录
|
79
|
|
if(savePath == null){
|
80
|
|
savePath = "USER_NAME".equals(name) ? path + value : tempPath;
|
81
|
|
}
|
82
|
|
}else{
|
83
|
|
//获取路径名
|
84
|
|
String value = item.getName();
|
85
|
|
int start = value.lastIndexOf("\\");
|
86
|
|
String filename = value.substring(start + 1);
|
87
|
|
//保存文件名
|
88
|
|
request.setAttribute(name, filename);
|
89
|
|
|
90
|
|
//检查保存目录
|
91
|
|
File saveDir = new File(savePath);
|
92
|
|
if(! saveDir.exists() && ! saveDir.mkdirs()){
|
93
|
|
throw new RuntimeException("生成上传保存文件夹失败,无法进行上传操作!");
|
94
|
|
}
|
95
|
|
//设置保存文件
|
96
|
|
File saveFile = new File(savePath, filename);
|
97
|
|
//写到磁盘上
|
98
|
|
OutputStream out = new FileOutputStream(saveFile);
|
99
|
|
InputStream in = item.getInputStream();
|
100
|
|
|
101
|
|
int length = 0;
|
102
|
|
byte[] buf = new byte[1024];
|
103
|
|
//写出到文件中
|
104
|
|
while((length = in.read(buf) ) != -1) {
|
105
|
|
//在BUF数组中取出数据写到(输出流)磁盘上
|
106
|
|
out.write(buf, 0, length);
|
107
|
|
}
|
108
|
|
out.flush();
|
109
|
|
in.close();
|
110
|
|
out.close();
|
111
|
|
|
112
|
|
log.debug("获取上传文件的总共的容量:" + item.getSize());
|
113
|
|
result.put("size", item.getSize());
|
114
|
|
}
|
115
|
|
}
|
116
|
|
} catch (FileUploadException e) {
|
117
|
|
e.printStackTrace();
|
118
|
|
|
119
|
|
}catch (Exception e) {
|
120
|
|
e.printStackTrace();
|
121
|
|
}
|
122
|
|
return result;
|
123
|
|
}
|
124
|
|
/************************************************************************************
|
125
|
|
* 文件下载;
|
126
|
|
*
|
127
|
|
* @param param
|
128
|
|
* @return
|
129
|
|
* @throws Exception
|
130
|
|
*/
|
131
|
|
public IData download(IData param) throws Exception{
|
132
|
|
HttpServletRequest request = ServletManager.getRequest();
|
133
|
|
HttpServletResponse response = ServletManager.getResponse();
|
134
|
|
//获取文件需要上传到的路径
|
135
|
|
String fileName = request.getParameter("FILE_PATH");
|
136
|
|
String mineType = request.getParameter("MINE_TYPE");
|
137
|
|
log.debug("MINE类型为:[" + mineType + "]");
|
138
|
|
// log.debug(request.getParameter("USER_NAME"));
|
139
|
|
// log.debug(request.getParameter("SESSION_ID"));
|
140
|
|
//获取文件地址
|
141
|
|
String filePath = ApplicationPath.getFilePath(request) + "upload" + File.separator + fileName;
|
142
|
|
File file = new File(filePath);
|
143
|
|
if(! file.isFile())
|
144
|
|
throw new RuntimeException("上传的文件不存在,请检查文件路径!");
|
145
|
|
|
146
|
|
response.reset();
|
147
|
|
response.setContentType(mineType);
|
148
|
|
InputStream in = new FileInputStream(file);
|
149
|
|
|
150
|
|
int length = 0;
|
151
|
|
byte[] buf = new byte[1024];
|
152
|
|
//写出到文件中
|
153
|
|
OutputStream out = response.getOutputStream();
|
154
|
|
while((length = in.read(buf) ) != -1) {
|
155
|
|
//在BUF数组中取出数据写到(输出流)磁盘上
|
156
|
|
out.write(buf, 0, length);
|
157
|
|
}
|
158
|
|
out.flush();
|
159
|
|
in.close();
|
160
|
|
out.close();
|
161
|
|
response.flushBuffer();
|
162
|
|
|
163
|
|
return null;
|
164
|
|
}
|
165
|
|
}
|