您的位置:首页 > 产品设计 > UI/UE

将前台的一个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;

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐