Commit 5c67c788 authored by dahai's avatar dahai

push

parent ededd8f3
...@@ -4,16 +4,25 @@ import com.yxproject.start.entity.FilesEntity; ...@@ -4,16 +4,25 @@ import com.yxproject.start.entity.FilesEntity;
import com.yxproject.start.entity.PreproPersonEntity; import com.yxproject.start.entity.PreproPersonEntity;
import com.yxproject.start.service.ImportXmlService; import com.yxproject.start.service.ImportXmlService;
import com.yxproject.start.utils.IDCardFactory; import com.yxproject.start.utils.IDCardFactory;
import com.yxproject.start.utils.YXJSONResponse;
import com.yxproject.start.utils.YXStringUtils;
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.dom4j.DocumentException;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.io.BufferedOutputStream; import javax.ws.rs.Consumes;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
...@@ -26,123 +35,92 @@ public class ImportXmlApi { ...@@ -26,123 +35,92 @@ public class ImportXmlApi {
/** /**
* 导入检测XML * 导入检测XML
* @param resp 响应请求
* @param requ 获得请求
* @return 成功返回 上传完成 失败返回 异常信息 * @return 成功返回 上传完成 失败返回 异常信息
*/ */
// @RequestMapping(value="getXMLToCheck", method= RequestMethod.POST) @Consumes(MediaType.MULTIPART_FORM_DATA)
// @ResponseBody @Produces(MediaType.APPLICATION_JSON)
@PostMapping("/batchUpload") @RequestMapping("getXMLToCheck")
public String getXMLToCheck( HttpServletResponse response,HttpServletRequest request) { public String getXMLToCheck(@Context HttpServletResponse resp, @Context HttpServletRequest requ) {
//定义处理流对象,处理文件上传 System.out.println("输入:"+requ);
BufferedOutputStream stream = null; System.out.println("输入2:"+resp);
//定义map存储返回结果集 YXJSONResponse yxresp = new YXJSONResponse();
Map<String,String> returnfileMap = new HashMap<String, String>(); resp.setCharacterEncoding("UTF-8");
//获取前端上传的文件列表 String filename = "";
List<MultipartFile> files = ((MultipartHttpServletRequest) request).getFiles("file"); DiskFileItemFactory factory = new DiskFileItemFactory();
MultipartFile file = null; // 设置缓冲区的大小为100KB,如果不指定,那么缓冲区的大小默认是10KB
factory.setSizeThreshold(1024 * 100);
//遍历客户端上传文件列表 ServletFileUpload upload = new ServletFileUpload(factory);
for (int i = 0; i < files.size(); ++i) { upload.setHeaderEncoding("UTF-8");
//获取到每个文件 // 设置上传单个文件的大小的最大值,目前是设置为1024*1024*10字节,也就是10MB
file = files.get(i); upload.setFileSizeMax(1024 * 1024 * 10);
try { // 设置上传文件总量的最大值,最大值=同时上传的多个文件的大小的最大值的和,目前设置为4000MB
//将上传文件保存到服务器上传文件夹目录下 upload.setSizeMax(1024 * 1024 * 4000);
byte[] bytes = file.getBytes(); // 将普通属性存入map中,之后调用
String str = new String(bytes); Map<String, String> map = new HashMap<String, String>();
IDCardFactory idCardFactory = new IDCardFactory(); List<FileItem> list = null;
idcardsFactory(idCardFactory.extractIDCard(str)); try {
} catch (Exception e) { list = upload.parseRequest(requ);
//保存上传失败的文件信息,将上传文件名作为key,value值为"fail",存入returnfileMap中
returnfileMap.put(file.getOriginalFilename(),"fail"); for (FileItem item : list) {
}finally { try {
// 如果fileitem中封装的是普通输入项的数据
if (item.isFormField()) {
String name = item.getFieldName();
// 解决普通输入项的数据的中文乱码问题
String value = item.getString("UTF-8");
// value = new String(value.getBytes("iso8859-1"),"UTF-8");
map.put(name, value);
} else {
// 如果fileitem中封装的是上传文件
// 得到上传的文件名称,
filename = item.getName();
if (filename == null || filename.trim().equals("")) {
continue;
}
InputStream in = item.getInputStream();
String str = YXStringUtils.inputStream2String(in, "utf-8");
in.close();
IDCardFactory idCardFactory = new IDCardFactory();
idcardsFactory(idCardFactory.extractIDCard(str));
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
yxresp.outPutError("UnsupportedEncodingException", "上传文件时发现文件编码错误:" + e.getMessage());
continue;
} catch (IOException e) {
e.printStackTrace();
yxresp.outPutError("IOException", "上传文件时发生IO错误:" + e.getMessage());
continue;
} catch (DocumentException e) {
e.printStackTrace();
yxresp.outPutError("DocumentException", "上传文件时发生IO错误:" + e.getMessage());
continue;
} catch (Exception e) {
e.printStackTrace();
yxresp.outPutError("Exception", "上传文件时发生错误,非法XML文件:" + filename);
continue;
}
} }
} catch (FileUploadException e) {
e.printStackTrace();
yxresp.outPutError("FileUploadException", "文件上传发生异常:" + e.getMessage());
} finally {
return yxresp.toJSONString();
} }
return null;
// YXJSONResponse yxresp = new YXJSONResponse();
// String filename = "";
//
// DiskFileItemFactory factory = new DiskFileItemFactory();
// // 设置缓冲区的大小为100KB,如果不指定,那么缓冲区的大小默认是10KB
// factory.setSizeThreshold(1024 * 100);
//
// ServletFileUpload upload = new ServletFileUpload(factory);
// upload.setHeaderEncoding("UTF-8");
// // 设置上传单个文件的大小的最大值,目前是设置为1024*1024*10字节,也就是10MB
// upload.setFileSizeMax(1024 * 1024 * 10);
// // 设置上传文件总量的最大值,最大值=同时上传的多个文件的大小的最大值的和,目前设置为4000MB
// upload.setSizeMax(1024 * 1024 * 4000);
// // 将普通属性存入map中,之后调用
// Map<String, String> map = new HashMap<String, String>();
// List<FileItem> list = null;
// try {
// list = upload.parseRequest(request);
//
// for (FileItem item : list) {
// try {
// // 如果fileitem中封装的是普通输入项的数据
// if (item.isFormField()) {
// String name = item.getFieldName();
// // 解决普通输入项的数据的中文乱码问题
// String value = item.getString("UTF-8");
// // value = new String(value.getBytes("iso8859-1"),"UTF-8");
// map.put(name, value);
// } else {
// // 如果fileitem中封装的是上传文件
// // 得到上传的文件名称,
// filename = item.getName();
//
// if (filename == null || filename.trim().equals("")) {
// continue;
// }
//
// InputStream in = item.getInputStream();
// String str = YXStringUtils.inputStream2String(in, "utf-8");
// in.close();
// IDCardFactory idCardFactory = new IDCardFactory();
// idcardsFactory(idCardFactory.extractIDCard(str));
//
// }
// } catch (UnsupportedEncodingException e) {
// e.printStackTrace();
//
// yxresp.outPutError("UnsupportedEncodingException", "上传文件时发现文件编码错误:" + e.getMessage());
// continue;
// } catch (IOException e) {
// e.printStackTrace();
// yxresp.outPutError("IOException", "上传文件时发生IO错误:" + e.getMessage());
// continue;
// } catch (DocumentException e) {
// e.printStackTrace();
// yxresp.outPutError("DocumentException", "上传文件时发生IO错误:" + e.getMessage());
// continue;
// } catch (Exception e) {
// e.printStackTrace();
// yxresp.outPutError("Exception", "上传文件时发生错误,非法XML文件:" + filename);
// continue;
// }
// }
// } catch (FileUploadException e) {
// e.printStackTrace();
// yxresp.outPutError("FileUploadException", "文件上载发生异常:" + e.getMessage());
// } finally {
// return yxresp.toJSONString();
// }
} }
private Boolean idcardsFactory(Map<String, Object> map1) { private Boolean idcardsFactory(Map<String, Object> map1) {
importXmlService.importPersonXml ((List<PreproPersonEntity>) map1.get("preproPerson"), (FilesEntity) map1.get("file")); importXmlService.importPersonXml ((List<PreproPersonEntity>) map1.get("preproPerson"), (FilesEntity) map1.get("file"));
return true; return true;
} }
......
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