Commit e5aaeba2 by fukai

支持上传

parent fb1e153f
package org.sss.presentation.noui.controller;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import log.Log;
import log.LogFactory;
import org.apache.commons.fileupload.FileItemIterator;
import org.apache.commons.fileupload.FileItemStream;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.fileupload.util.Streams;
import org.apache.commons.io.IOUtils;
import org.sss.common.model.EventType;
import org.sss.common.model.IBaseObject;
......@@ -39,6 +34,11 @@ import org.sss.presentation.noui.util.NoUiPresentationUtil;
import org.sss.presentation.noui.util.RedisUtil;
import org.sss.presentation.noui.util.StringUtil;
import com.google.gson.Gson;
import log.Log;
import log.LogFactory;
public class AbstractCommonController {
protected static final Log log = LogFactory.getLog(AbstractCommonController.class);
protected static String ON_CLICK = "ON_CLICK";
......@@ -53,25 +53,27 @@ public class AbstractCommonController {
NoUiContext context = null;
Result ret = null;
boolean isMultipart = false;
InputStream fileStream = null;
FileItem lastFileItem = null;
try {
isMultipart = ServletFileUpload.isMultipartContent(request);
if (isMultipart) {
ServletFileUpload upload = new ServletFileUpload();
ServletFileUpload upload = new ServletFileUpload(new DiskFileItemFactory());
// Parse the request
FileItemIterator iter = upload.getItemIterator(request);
List<FileItem> items = upload.parseRequest(request);
Iterator<FileItem> iter = items.iterator();
while (iter.hasNext()) {
FileItemStream item = iter.next();
FileItem item = iter.next();
// 普通表单域
if (item.isFormField()) {
String itemName = item.getFieldName();
String itemValue = Streams.asString(item.openStream());
String itemValue =item.getString();
// 给dataMap赋值
dataMap.put(itemName, itemValue);
Map<String,?> itemMap = new Gson().fromJson(itemValue, Map.class);
dataMap.put(itemName, itemMap);
} else { // 文件域
fileStream = item.openStream();
lastFileItem = item;
}
}
......@@ -112,23 +114,15 @@ public class AbstractCommonController {
((IDatafield<?>) currentDataField).invokeCheckRules(context);
}
}
} else if (eventType.equals(ON_STREAM_UPLOAD)) {
} else if (eventType.equals(ON_STREAM_UPLOAD) && lastFileItem!=null) {
@SuppressWarnings("rawtypes")
IDatafield dataField = (IDatafield) baseObject(context, noUiRequest, alias);
IStream stream = (IStream) dataField.getValue();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] data = new byte[1024];
int bytes = 0;
if (fileStream != null) {
while ((bytes = fileStream.read(data)) != -1) {
bos.write(data, 0, bytes);
}
IOUtils.write(bos.toByteArray(), stream.getOutputStream());
IOUtils.closeQuietly(bos);
}
stream.setName(lastFileItem.getName());
stream.setType(lastFileItem.getContentType());
IOUtils.copy(lastFileItem.getInputStream(), stream.getOutputStream(), 1024);
// dataField.setValue(stream);
dataField.invokeEventRules(context, EventType.ON_STREAM_UPLOAD, null);
IOUtils.closeQuietly(fileStream);
} else if (eventType.equals(ON_STREAM_DOWNLOAD)) {
@SuppressWarnings("rawtypes")
IDatafield dataField = (IDatafield) baseObject(context, noUiRequest, alias);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment