Commit 5c67c788 authored by dahai's avatar dahai

push

parent ededd8f3
......@@ -4,16 +4,25 @@ import com.yxproject.start.entity.FilesEntity;
import com.yxproject.start.entity.PreproPersonEntity;
import com.yxproject.start.service.ImportXmlService;
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.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
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.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.List;
import java.util.Map;
......@@ -26,123 +35,92 @@ public class ImportXmlApi {
/**
* 导入检测XML
* @param resp 响应请求
* @param requ 获得请求
* @return 成功返回 上传完成 失败返回 异常信息
*/
// @RequestMapping(value="getXMLToCheck", method= RequestMethod.POST)
// @ResponseBody
@PostMapping("/batchUpload")
public String getXMLToCheck( HttpServletResponse response,HttpServletRequest request) {
//定义处理流对象,处理文件上传
BufferedOutputStream stream = null;
//定义map存储返回结果集
Map<String,String> returnfileMap = new HashMap<String, String>();
//获取前端上传的文件列表
List<MultipartFile> files = ((MultipartHttpServletRequest) request).getFiles("file");
MultipartFile file = null;
//遍历客户端上传文件列表
for (int i = 0; i < files.size(); ++i) {
//获取到每个文件
file = files.get(i);
try {
//将上传文件保存到服务器上传文件夹目录下
byte[] bytes = file.getBytes();
String str = new String(bytes);
IDCardFactory idCardFactory = new IDCardFactory();
idcardsFactory(idCardFactory.extractIDCard(str));
} catch (Exception e) {
//保存上传失败的文件信息,将上传文件名作为key,value值为"fail",存入returnfileMap中
returnfileMap.put(file.getOriginalFilename(),"fail");
}finally {
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.APPLICATION_JSON)
@RequestMapping("getXMLToCheck")
public String getXMLToCheck(@Context HttpServletResponse resp, @Context HttpServletRequest requ) {
System.out.println("输入:"+requ);
System.out.println("输入2:"+resp);
YXJSONResponse yxresp = new YXJSONResponse();
resp.setCharacterEncoding("UTF-8");
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(requ);
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();
}
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) {
importXmlService.importPersonXml ((List<PreproPersonEntity>) map1.get("preproPerson"), (FilesEntity) map1.get("file"));
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