Commit f0a6d7aa authored by dahai's avatar dahai

导入个人邮寄信息

parent c7a73b19
...@@ -87,6 +87,11 @@ ...@@ -87,6 +87,11 @@
<artifactId>javax.ws.rs-api</artifactId> <artifactId>javax.ws.rs-api</artifactId>
<version>2.0</version> <version>2.0</version>
</dependency> </dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.3</version>
</dependency>
</dependencies> </dependencies>
......
...@@ -3,16 +3,24 @@ package com.yxproject.start.api; ...@@ -3,16 +3,24 @@ package com.yxproject.start.api;
import com.yxproject.start.entity.PersonPostEntity; import com.yxproject.start.entity.PersonPostEntity;
import com.yxproject.start.service.PersonPostService; import com.yxproject.start.service.PersonPostService;
import com.yxproject.start.utils.ReadExcel; import com.yxproject.start.utils.ReadExcel;
import com.yxproject.start.utils.YXJSONResponse;
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.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
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 javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.io.File; import javax.ws.rs.core.Context;
import java.io.*;
import java.sql.Time; import java.sql.Time;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
/** /**
* *
...@@ -28,48 +36,112 @@ public class ReadExcelApi { ...@@ -28,48 +36,112 @@ public class ReadExcelApi {
Boolean result = null; Boolean result = null;
/** /**
* 导入个人信息表 * 导入个人邮寄信息表
*/ */
@RequestMapping("ReadPersonPost") @RequestMapping("ReadPersonPost")
public boolean ReadPersonPost(@Context HttpServletResponse resp, @Context HttpServletRequest requ){ public String ReadPersonPost(@Context HttpServletResponse resp, @Context HttpServletRequest requ){
ReadExcel obj = new ReadExcel(); YXJSONResponse yxresp = new YXJSONResponse();
PersonPostEntity personPost = new PersonPostEntity(); resp.setCharacterEncoding("UTF-8");
List<PersonPostEntity> entityList = new ArrayList<PersonPostEntity>(); String filename = "";
File file = new File("E:/北京制证项目/Person_Post.xls"); DiskFileItemFactory factory = new DiskFileItemFactory();
List excelList = obj.readExcel(file); // 设置缓冲区的大小为100KB,如果不指定,那么缓冲区的大小默认是10KB
for (int i=0;i<excelList.size();i++){ factory.setSizeThreshold(1024 * 100);
List list = (List) excelList.get(i); ServletFileUpload upload = new ServletFileUpload(factory);
//跳过空行和第一行 upload.setHeaderEncoding("UTF-8");
if (list == null||i==0){ // 设置上传单个文件的大小的最大值,目前是设置为1024*1024*10字节,也就是10MB
continue; 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) {
File file =null;
try {
// 如果fileitem中封装的是普通输入项的数据
if (item.isFormField()) {
String name = item.getFieldName();
// 解决普通输入项的数据的中文乱码问题
String value = item.getString("UTF-8");
map.put(name, value);
} else {
// 如果fileitem中封装的是上传文件
// 得到上传的文件名称,
filename = item.getName();
if (filename == null || filename.trim().equals("")) {
continue;
}
InputStream in = item.getInputStream();
OutputStream os = new FileOutputStream(file);
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = in.read(buffer, 0, 8192)) != -1) {
os.write(buffer, 0, bytesRead);
}
os.close();
in.close();
}
ReadExcel readExcel = new ReadExcel();
List list1 = readExcel.readExcel(file);
List<PersonPostEntity> entityList= new ArrayList<PersonPostEntity>();
for (int i = 0; i < list1.size(); i++){
PersonPostEntity personPostEntity = new PersonPostEntity();
List list2 = (List) list1.get(i);
//跳过空行和第一行
if (list2 == null||i==0){
continue;
}
personPostEntity.setBackWaybillNumber((String) list2.get(0));
personPostEntity.setOrderNumber((String) list2.get(1));
personPostEntity.setCreateDate((Time) list2.get(2));
personPostEntity.setOpenid((String) list2.get(3));
personPostEntity.setWcPlayOrderNumber((String) list2.get(4));
personPostEntity.setPlayState((String) list2.get(5));
personPostEntity.setOrderState((String) list2.get(6));
personPostEntity.setApplicantName((String) list2.get(7));
personPostEntity.setSenderName((String) list2.get(8));
personPostEntity.setSenderPhone((String) list2.get(9));
personPostEntity.setSenderAddress((String) list2.get(10));
personPostEntity.setRecipientName((String) list2.get(11));
personPostEntity.setRecipientPhone((String) list2.get(12));
personPostEntity.setRecipientAddress((String) list2.get(13));
personPostEntity.setOrderBlankNumber((String) list2.get(14));
personPostEntity.setGetToProvince((String) list2.get(15));
personPostEntity.setGetToCity((String) list2.get(16));
personPostEntity.setGetToCounty((String) list2.get(17));
personPostEntity.setBusinessType((String) list2.get(18));
personPostEntity.setLatticeMouthInformation((String) list2.get(19));
personPostEntity.setNatureOfTheInternal((String) list2.get(20));
personPostEntity.setNatureOfTheInformation((String) list2.get(21));
personPostEntity.setFirstWhite((String) list2.get(22));
entityList.add(personPostEntity);
}
boolean b = personPostService.savePersonPost(entityList);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
yxresp.outPutError("UnsupportedEncodingException", "上传文件时发现文件编码错误:" + e.getMessage());
continue;
} catch (IOException e) {
e.printStackTrace();
yxresp.outPutError("IOException", "上传文件时发生IO错误:" + e.getMessage());
continue;
} catch (Exception e) {
e.printStackTrace();
yxresp.outPutError("Exception", "上传文件时发生错误,非法XML文件:" + filename);
continue;
}
} }
personPost.setBackWaybillNumber((String) list.get(0)); } catch (FileUploadException e) {
personPost.setOrderNumber((String) list.get(1)); e.printStackTrace();
personPost.setCreateDate((Time) list.get(2)); yxresp.outPutError("FileUploadException", "文件上载发生异常:" + e.getMessage());
personPost.setOpenid((String) list.get(3)); } finally {
personPost.setWcPlayOrderNumber((String) list.get(4)); return yxresp.toJSONString();
personPost.setPlayState((String) list.get(5));
personPost.setOrderState((String) list.get(6));
personPost.setApplicantName((String) list.get(7));
personPost.setSenderName((String) list.get(8));
personPost.setSenderPhone((String) list.get(9));
personPost.setSenderAddress((String) list.get(10));
personPost.setRecipientName((String) list.get(11));
personPost.setRecipientPhone((String) list.get(12));
personPost.setRecipientAddress((String) list.get(13));
personPost.setOrderBlankNumber((String) list.get(14));
personPost.setGetToProvince((String) list.get(15));
personPost.setGetToCity((String) list.get(16));
personPost.setGetToCounty((String) list.get(17));
personPost.setBusinessType((String) list.get(18));
personPost.setLatticeMouthInformation((String) list.get(19));
personPost.setNatureOfTheInternal((String) list.get(20));
personPost.setLatticeMouthInformation((String) list.get(21));
personPost.setFirstWhite((String) list.get(22));
entityList.add(personPost);
} }
result = personPostService.updatePersonPost(entityList);
return result;
} }
} }
...@@ -10,7 +10,7 @@ import org.apache.ibatis.annotations.Update; ...@@ -10,7 +10,7 @@ import org.apache.ibatis.annotations.Update;
*/ */
@Mapper @Mapper
public interface PersonPostMapper { public interface PersonPostMapper {
@Update("update PERSON_POST set WAYBILL_NUMBER=#{waybillNumber},BACK_WAYBILL_NUMBER=#{backWaybillNumber},ORDER_NUMBER=#{orderNumber},CREATE_DATE=#{createDate},OPENID=#{openid},WC_PLAY_ORDER_NUMBER=#{wcPlayOrderNumber},PLAY_STATE=#{playState},ORDER_STATE=#{orderState},,APPLICANT_NAME=#{applicantName},SENDER_NAME=#{senderName},SENDER_PHONE=#{senderPhone},SENDER_ADDRESS=#{senderAddress},RECIPIENT_NAME=#{recipientName},RECIPIENT_PHONE=#{recipientPhone},RECIPIENT_ADDRESS=#{recipientAddress},ORDER_BLANK_NUMBER=#{orderBlankNumber},GET_TO_PROVINCE=#{getToProvince},GET_TO_CITY=#{getToCity},GET_TO_COUNTY=#{getToCounty},BUSINESS_TYPE=#{businessType},LATTICE_MOUTH_INFORMATION=#{latticeMouthInformation},NATURE_OF_THE_INTERNAL=#{natureOfTheInternal},NATURE_OF_THE_INFORMATION=#{natureOfTheInformation},FIRST_WHITE=#{firstWhite}") @Update("insert into PERSON_POST ( WAYBILL_NUMBER,BACK_WAYBILL_NUMBER,ORDER_NUMBER,CREATE_DATE,OPENID,WC_PLAY_ORDER_NUMBER,PLAY_STATE,ORDER_STATE,APPLICANT_NAME,SENDER_NAME,SENDER_PHONE,SENDER_ADDRESS,RECIPIENT_NAME,RECIPIENT_PHONE,RECIPIENT_ADDRESS,ORDER_BLANK_NUMBER,GET_TO_PROVINCE,GET_TO_CITY,GET_TO_COUNTY,BUSINESS_TYPE,LATTICE_MOUTH_INFORMATION,NATURE_OF_THE_INTERNAL,NATURE_OF_THE_INFORMATION,FIRST_WHITE,UPLOAD_DATE) values (#{waybillNumber},#{backWaybillNumber},#{orderNumber},#{createDate},#{openid},#{wcPlayOrderNumber},#{playState},#{orderState},#{applicantName},#{senderName},#{senderPhone},#{senderAddress},#{recipientName},#{recipientPhone},#{recipientAddress},#{orderBlankNumber},#{getToProvince},#{getToCity},#{getToCounty},#{businessType},#{latticeMouthInformation},#{natureOfTheInternal},#{natureOfTheInformation},#{firstWhite},#(uploadDate))")
public boolean updatePersonPost(PersonPostEntity personPostEntity); public boolean savePersonPost(PersonPostEntity personPostEntity);
} }
...@@ -12,5 +12,5 @@ public interface PersonPostService { ...@@ -12,5 +12,5 @@ public interface PersonPostService {
/** /**
* 导入个人邮寄异常信息 * 导入个人邮寄异常信息
*/ */
public boolean updatePersonPost(List<PersonPostEntity> personPostEntities); public boolean savePersonPost(List<PersonPostEntity> personPostEntities);
} }
...@@ -9,6 +9,8 @@ import org.springframework.beans.factory.annotation.Autowired; ...@@ -9,6 +9,8 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List; import java.util.List;
/** /**
...@@ -23,13 +25,10 @@ public class PersonPostServiceImpl implements PersonPostService { ...@@ -23,13 +25,10 @@ public class PersonPostServiceImpl implements PersonPostService {
private PreproPersonMapper preproPersonMapper; private PreproPersonMapper preproPersonMapper;
@Override @Override
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public boolean updatePersonPost(List<PersonPostEntity> personPostEntities) { public boolean savePersonPost(List<PersonPostEntity> personPostEntities) {
for (PersonPostEntity personPostEntity: personPostEntities) { for (PersonPostEntity personPostEntity: personPostEntities) {
boolean b = personPostMapper.updatePersonPost(personPostEntity); personPostEntity.setCreateDate(new java.sql.Time(new Date().getTime()));
PreproPersonEntity preproPersonEntity = new PreproPersonEntity(); boolean b = personPostMapper.savePersonPost(personPostEntity);
preproPersonEntity.setJmsfzslh(personPostEntity.getFirstWhite());
preproPersonEntity.setIsPost((long)1);
preproPersonMapper.updatePreproPersonEntity(preproPersonEntity);
} }
return true; return true;
} }
......
package com.yxproject.start.utils; package com.yxproject.start.utils;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import jxl.Sheet; import jxl.Sheet;
import jxl.Workbook; import jxl.Workbook;
import jxl.read.biff.BiffException; import jxl.read.biff.BiffException;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
/** /**
* Created by zhangyusheng on 2019/1/30 14:39 * Created by zhangyusheng on 2019/1/30 14:39
*/ */
public class ReadExcel { public class ReadExcel {
public static void main(String[] args) {
ReadExcel obj = new ReadExcel();
// 此处为我创建Excel路径:E:/zhanhj/studysrc/jxl下
File file = new File("E:/北京制证项目/personal_progress_status.xls");
List excelList = obj.readExcel(file);
System.out.println("list中的数据打印出来");
for (int i = 0; i < excelList.size(); i++) {
System.out.println("第"+i+"行:"+excelList.get(i));
List list = (List) excelList.get(i);
for (int j = 0; j < list.size(); j++) {
// System.out.print(list.get(j));+
}
}
}
// 去读Excel的方法readExcel,该方法的入口参数为一个File对象 // 去读Excel的方法readExcel,该方法的入口参数为一个File对象
public List readExcel(File file) { public List readExcel(File file) {
try { try {
// 创建输入流,读取Excel // 创建输入流,读取Excel
InputStream is = new FileInputStream(file.getAbsolutePath()); InputStream is = new FileInputStream(file.getAbsolutePath());
......
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