求大神解释啊。急求急求!!!!!!
tomcat 服务器
------解决方案--------------------
自己学一下吧http://blog.sina.com.cn/s/blog_60afee6e010136dc.html
------解决方案--------------------
protected Map<String, Object> parseMultipart(HttpServletRequest request) {
Map<String, Object> map = new HashMap<String, Object>();
RequestContext ctx = new ServletRequestContext((HttpServletRequest) request);
if (!FileUpload.isMultipartContent(ctx))
return map;
try {
DiskFileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setSizeMax(1024 * 1024 * 5);// 设置文件限制为5M
List<FileItem> items = upload.parseRequest((HttpServletRequest) request);
if (items != null) {
Map<String, List<Object>> tmp = new HashMap<String, List<Object>>();
for (FileItem item : items) {
Object value = item.isFormField() ? item.getString("UTF-8") : item;
if (!tmp.containsKey(item.getFieldName())) {
List<Object> values = new ArrayList<Object>();
tmp.put(item.getFieldName(), values);
}
tmp.get(item.getFieldName()).add(value);
}
if (tmp.size() > 0) {
for (String key : tmp.keySet()) {
List<Object> values = tmp.get(key);
if (values.size() == 1) {
map.put(key, values.get(0));
} else {
map.put(key, values.toArray());
}
}
}
}
} catch (Exception e) {
logger.error(e);
}
return map;
}
------解决方案--------------------
public void fileUpload2() {
String result = "OK";
// upload.setSizeMax(1024 * 1024 * 5);
HttpServletRequest request = ServletActionContext
.getRequest();
String appName = request.getContextPath();
// 上传文件夹路径
String path = "../webapps" + appName + "/files/";
// 返回上传图片的网络地址
String basePath = request.getScheme() + "://" + request.getServerName()
+ ":" + request.getServerPort() + appName + "/" + "files/";
String httpurl="";
try {
String fname =this.getFileStream2FileName();
//System.out.println(fname);
//System.out.println("$$$$$$$$$$$$$$$"+fileStream2.length);
String[] fnameList=fname.split(",");
for(int i=0;i<fileStream2.length;i++){
String path1 = path + fnameList[i].trim();
String onrepath = basePath + fnameList[i].trim();
httpurl=httpurl+onrepath+",";
File f = new File(path1);
if (!f.getParentFile().exists())
f.getParentFile().mkdirs();
f.createNewFile();
FileOutputStream fi = new FileOutputStream(f);
InputStream in = new FileInputStream(fileStream2[i]);;
byte buffer[] = new byte[8192];
int bytesRead = 0;
while ((bytesRead = in.read(buffer, 0, 8192)) != -1) {
fi.write(buffer, 0, bytesRead);
}
in.close();
fi.close();
}
} catch (Exception e) {
e.printStackTrace();
result = "ERROR:Exception,请联系管理员!";
}
HttpServletResponse res = ServletActionContext.getResponse();
if ("OK".equals(result)) {
putJsondata(httpurl, res);
//System.out.println("333333333#################################"+httpurl);
} else {
putJsondata(result, res);
}
}