将前台的一个form转换成一个list,list中包含了两个map, 一个是form的input字段key和value的map,另一个是附件map
2010-05-26 18:06
477 查看
package com.css.yj.yjzs.ctrlCommon;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
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 org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.log4j.Logger;
/**
* 将前台的一个form转换成一个list,list中包含了两个map, 一个是form的input字段key和value的map,另一个是附件map
*
* @author libin
* @param request
* @return
*/
public class FileUpLoadParaUtil {
private static Logger logger = Logger.getLogger(FileUpLoadParaUtil.class
.getName());
// 默认文件上传大小限制200M
public static int FILE_SIZE_THRESHOLD = 1024 * 1024 * 2;
public static int FILE_SIZE_MAX = 1024 * 1024 * 50;
// 默认文件编码方式UTF-8
public static String FILE_ENCODING = "UTF-8";
public static DiskFileItemFactory factory = new DiskFileItemFactory();
public static ServletFileUpload upload = null;
@SuppressWarnings("unchecked")
public static List<Map> requestToList(HttpServletRequest request)
throws FileUploadException, IOException {
// 获得上传信息
List list = new ArrayList();
Map fieldMap = new HashMap();
Map<String, byte[]> fjFileMap = new HashMap<String, byte[]>();
List<FileItem> items;
factory.setSizeThreshold(FILE_SIZE_THRESHOLD);
upload = new ServletFileUpload(factory);
upload.setSizeMax(FILE_SIZE_MAX);
upload.setHeaderEncoding(FILE_ENCODING);
try {
items = upload.parseRequest(request);
} catch (FileUploadException e) {
e.printStackTrace();
logger.error(e.getMessage());
throw new FileUploadException("文件上传失败");
}
Iterator iter = items.iterator();
int fileCount=0;
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
// 如果是form中的字段
if (item.isFormField()) {
String key = item.getFieldName();
String value;
try {
value = new String(item.getString().getBytes("iso-8859-1"),
"utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
logger.error("编码转换失败");
throw new UnsupportedEncodingException("编码转换失败");
}
logger.debug("key is: " + key + " value is: " + value);
if (key.equals("xxsjlxmc")) {
// 对xxsjlxmc进行特殊处理,只要最后一个有效的“信息类型”的名称
value = value.replaceAll("请选择", "");
value = value.replaceAll(",", "=");
logger.debug("value is: " + value);
String[] arrayXxsjlxmc = value.split("="); // value的格式为:请选择,,
logger.debug("arrayXxsjlxmc length is: "
+ arrayXxsjlxmc.length);
for (int i = arrayXxsjlxmc.length - 1; i >= 0; i--) {
if (!arrayXxsjlxmc[i].equals("")) {
fieldMap.put(key, arrayXxsjlxmc[i]);
break;
}
}
} else {
fieldMap.put(key, value);
}
}
// 如果是文件上传
else {
// 获取文件完整路径
String inputFilePath = item.getName();
// 获取文件名称
if (!inputFilePath.equals("")) {
String[] fileNameArray = inputFilePath.split("////");
String inputFileRealName = fileNameArray[fileNameArray.length - 1];
// 文件输入流
InputStream fis = null;
ByteArrayOutputStream os = null;
byte[] buffer = new byte[1024];
try {
fis = item.getInputStream();
os = new ByteArrayOutputStream();
for (int count = 0;(count = fis.read(buffer)) > 0;) {
os.write(buffer, 0, count);
// System.out.println(count);
os.flush();
}
fileCount=fileCount+1;
String fileMapKey="fileName-key"+Integer.toString(fileCount);
String filecontent="fileName-content"+Integer.toString(fileCount);
fieldMap.put(fileMapKey, inputFileRealName);
fjFileMap.put(filecontent, os.toByteArray());
fis.close();
os.close();
} catch (IOException e) {
e.printStackTrace();
logger.error(e.getMessage());
throw new IOException("io 失败");
}
}
}
}
logger.debug("fieldMap is ---------------" + fileCount);
logger.debug("fjFileMap is ---------------" + fjFileMap);
list.add(fieldMap);
list.add(fjFileMap);
return list;
}
}
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
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 org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.log4j.Logger;
/**
* 将前台的一个form转换成一个list,list中包含了两个map, 一个是form的input字段key和value的map,另一个是附件map
*
* @author libin
* @param request
* @return
*/
public class FileUpLoadParaUtil {
private static Logger logger = Logger.getLogger(FileUpLoadParaUtil.class
.getName());
// 默认文件上传大小限制200M
public static int FILE_SIZE_THRESHOLD = 1024 * 1024 * 2;
public static int FILE_SIZE_MAX = 1024 * 1024 * 50;
// 默认文件编码方式UTF-8
public static String FILE_ENCODING = "UTF-8";
public static DiskFileItemFactory factory = new DiskFileItemFactory();
public static ServletFileUpload upload = null;
@SuppressWarnings("unchecked")
public static List<Map> requestToList(HttpServletRequest request)
throws FileUploadException, IOException {
// 获得上传信息
List list = new ArrayList();
Map fieldMap = new HashMap();
Map<String, byte[]> fjFileMap = new HashMap<String, byte[]>();
List<FileItem> items;
factory.setSizeThreshold(FILE_SIZE_THRESHOLD);
upload = new ServletFileUpload(factory);
upload.setSizeMax(FILE_SIZE_MAX);
upload.setHeaderEncoding(FILE_ENCODING);
try {
items = upload.parseRequest(request);
} catch (FileUploadException e) {
e.printStackTrace();
logger.error(e.getMessage());
throw new FileUploadException("文件上传失败");
}
Iterator iter = items.iterator();
int fileCount=0;
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
// 如果是form中的字段
if (item.isFormField()) {
String key = item.getFieldName();
String value;
try {
value = new String(item.getString().getBytes("iso-8859-1"),
"utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
logger.error("编码转换失败");
throw new UnsupportedEncodingException("编码转换失败");
}
logger.debug("key is: " + key + " value is: " + value);
if (key.equals("xxsjlxmc")) {
// 对xxsjlxmc进行特殊处理,只要最后一个有效的“信息类型”的名称
value = value.replaceAll("请选择", "");
value = value.replaceAll(",", "=");
logger.debug("value is: " + value);
String[] arrayXxsjlxmc = value.split("="); // value的格式为:请选择,,
logger.debug("arrayXxsjlxmc length is: "
+ arrayXxsjlxmc.length);
for (int i = arrayXxsjlxmc.length - 1; i >= 0; i--) {
if (!arrayXxsjlxmc[i].equals("")) {
fieldMap.put(key, arrayXxsjlxmc[i]);
break;
}
}
} else {
fieldMap.put(key, value);
}
}
// 如果是文件上传
else {
// 获取文件完整路径
String inputFilePath = item.getName();
// 获取文件名称
if (!inputFilePath.equals("")) {
String[] fileNameArray = inputFilePath.split("////");
String inputFileRealName = fileNameArray[fileNameArray.length - 1];
// 文件输入流
InputStream fis = null;
ByteArrayOutputStream os = null;
byte[] buffer = new byte[1024];
try {
fis = item.getInputStream();
os = new ByteArrayOutputStream();
for (int count = 0;(count = fis.read(buffer)) > 0;) {
os.write(buffer, 0, count);
// System.out.println(count);
os.flush();
}
fileCount=fileCount+1;
String fileMapKey="fileName-key"+Integer.toString(fileCount);
String filecontent="fileName-content"+Integer.toString(fileCount);
fieldMap.put(fileMapKey, inputFileRealName);
fjFileMap.put(filecontent, os.toByteArray());
fis.close();
os.close();
} catch (IOException e) {
e.printStackTrace();
logger.error(e.getMessage());
throw new IOException("io 失败");
}
}
}
}
logger.debug("fieldMap is ---------------" + fileCount);
logger.debug("fjFileMap is ---------------" + fjFileMap);
list.add(fieldMap);
list.add(fjFileMap);
return list;
}
}
相关文章推荐
- 集合框架(一)续 向集合中添加一个集合、数组和List之间的相互转换、从集合中删除另一个集合所包含的元素
- 在map组成的List中发现修改一个map中的key-value,会影响導另一个
- 给定两个有着相同长度且都在字典内的单词,要求写一个方法来把一个单词变型成另一个单词。 一次只能转换一个字母,且每次生成的单词必须在字典内
- A,B两个页面,A里面用iframe包含B,现在在A里面要取得B里面一个Hidden控件的value
- 假设有两个包含整数的vector对象,编写一段程序,检验其中一个vector对象是否是另一个的前缀。
- 随机获取一个集合(List, Set)中的元素,随机获取一个Map中的key或value
- 比较两个数组中,其中一个数组不包含另一个数组的方法
- .有两个CIDR地址块208.128/11和208.130.28/22。是否有那一个地址块包含了另一个地址?如果有,请指出,并说明理由。
- 将一个List<Object>转换成json,方便前台界面获取
- Clojure:将两个list合并成一个map
- [转载]两个Form,一个Form中处理数据,另一个是ShowDialog()出来的上面就一个progressBar用来显示进度!!
- 链式A+B有两个用链表表示的整数,每个结点包含一个数位。这些数位是反向存放的,也就是个位排在链表的首部。编写函数对这两个整数求和,并用链表形式返回结果。 给定两个链表ListNode* A,ListN
- 类型转换——如何将一个List<Map<String,String>>类型换成json格式
- C#中一个窗体传递另一个窗体的值和sting类型到double类型的转换两个问题
- 将一个DataTable转换成一个List<T>的泛型集合
- 将List集合转换List数组,将List数组转换为String数组,将String字符串放到String数组中,将一个数组赋值给另一个数组
- 将一个字典内的内value转换为集合:返回一个数组,此数组中包含输入字典的键值对中的数组的所有元素(为NSArray添加category)
- 将一个DataTable转换成一个List<T>的泛型集合
- fastJson如何序列化一个包含list对象的Map对象,如Map<String,List<T>>类型的对象
- Map通过map.keyset或者Map.Entry遍历;map多个value对应一个key,逆向转成一个key对应一个list;通过邮编查多个地名