Commit c5112ae6 authored by dahai's avatar dahai

调试上传文件

parent 6d748685
...@@ -72,7 +72,6 @@ public class ImportXmlApi { ...@@ -72,7 +72,6 @@ public class ImportXmlApi {
if (multipartResolver.isMultipart(requ)) { if (multipartResolver.isMultipart(requ)) {
MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) requ; MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) requ;
Iterator<String> iter = multiRequest.getFileNames(); Iterator<String> iter = multiRequest.getFileNames();
Integer fileCount = 0;
while (iter.hasNext()) { while (iter.hasNext()) {
MultipartFile item = multiRequest.getFile(iter.next()); MultipartFile item = multiRequest.getFile(iter.next());
try { try {
......
...@@ -7,25 +7,27 @@ import com.yxproject.start.service.PersonPostService; ...@@ -7,25 +7,27 @@ 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 com.yxproject.start.utils.YXJSONResponse;
import org.apache.commons.fileupload.FileItem; import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory; import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload; import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
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 org.springframework.web.multipart.commons.CommonsMultipartResolver;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.core.Context; import javax.ws.rs.Consumes;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.io.*; import java.io.*;
import java.sql.Time; import java.sql.Time;
import java.util.*; import java.util.*;
/** /**
*
* 导入Excel文件 * 导入Excel文件
*
*/ */
@RestController @RestController
@RequestMapping("ReadExcel") @RequestMapping("ReadExcel")
...@@ -41,8 +43,10 @@ public class ReadExcelApi { ...@@ -41,8 +43,10 @@ public class ReadExcelApi {
* 导入个人邮寄信息表 * 导入个人邮寄信息表
*/ */
@RequestMapping("ReadPersonPost") @RequestMapping("ReadPersonPost")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.APPLICATION_JSON)
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public String ReadPersonPost(@Context HttpServletResponse resp, @Context HttpServletRequest requ){ public String ReadPersonPost(HttpServletResponse resp, HttpServletRequest requ) {
YXJSONResponse yxresp = new YXJSONResponse(); YXJSONResponse yxresp = new YXJSONResponse();
resp.setCharacterEncoding("UTF-8"); resp.setCharacterEncoding("UTF-8");
String filename = ""; String filename = "";
...@@ -59,98 +63,201 @@ public class ReadExcelApi { ...@@ -59,98 +63,201 @@ public class ReadExcelApi {
Map<String, String> map = new HashMap<String, String>(); Map<String, String> map = new HashMap<String, String>();
List<FileItem> list = null; List<FileItem> list = null;
try { try {
list = upload.parseRequest(requ);
for (FileItem item : list) {
File file =null; //将当前上下文初始化给 CommonsMutipartResolver (多部分解析器)
try { CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(
// 如果fileitem中封装的是普通输入项的数据 requ.getSession().getServletContext());
if (item.isFormField()) { // 判断是否是多数据段提交格式
String name = item.getFieldName(); if (multipartResolver.isMultipart(requ)) {
// 解决普通输入项的数据的中文乱码问题 MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) requ;
String value = item.getString("UTF-8"); Iterator<String> iter = multiRequest.getFileNames();
map.put(name, value); while (iter.hasNext()) {
} else { MultipartFile item = multiRequest.getFile(iter.next());
try {
// 如果fileitem中封装的是普通输入项的数据
// 如果fileitem中封装的是上传文件 // 如果fileitem中封装的是上传文件
// 得到上传的文件名称, // 得到上传的文件名称,
filename = item.getName(); filename = item.getName();
File file = null;
try {
// 如果fileitem中封装的是普通输入项的数据
// 如果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);
//保存EXCEL文件信息
FileNameDicEntity fileNameDicEntity = new FileNameDicEntity();
fileNameDicEntity.setFileName(filename);
fileNameDicEntity.setUploadDate(new Date());
//保存个人邮寄信息
long fileID = fileNameDicService.saveFileNameDic(fileNameDicEntity);
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));
personPostEntity.setFileId(fileID);
entityList.add(personPostEntity);
}
boolean b = personPostService.savePersonPost(entityList);
if (filename == null || filename.trim().equals("")) { } catch (UnsupportedEncodingException e) {
e.printStackTrace();
yxresp.outPutError("UnsupportedEncodingException", "上传文件时发现文件编码错误:" + e.getMessage());
continue; continue;
} } catch (IOException e) {
InputStream in = item.getInputStream(); e.printStackTrace();
OutputStream os = new FileOutputStream(file); yxresp.outPutError("IOException", "上传文件时发生IO错误:" + e.getMessage());
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);
//保存EXCEL文件信息
FileNameDicEntity fileNameDicEntity = new FileNameDicEntity();
fileNameDicEntity.setFileName(filename);
fileNameDicEntity.setUploadDate(new Date());
//保存个人邮寄信息
long fileID = fileNameDicService.saveFileNameDic(fileNameDicEntity);
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; continue;
} catch (Exception e) {
e.printStackTrace();
yxresp.outPutError("Exception", "上传文件时发生错误,非法XML文件:" + filename);
continue;
} finally {
} }
personPostEntity.setBackWaybillNumber((String) list2.get(0));
personPostEntity.setOrderNumber((String) list2.get(1)); // list = upload.parseRequest(requ);
personPostEntity.setCreateDate((Time) list2.get(2)); //
personPostEntity.setOpenid((String) list2.get(3)); // for (FileItem item : list) {
personPostEntity.setWcPlayOrderNumber((String) list2.get(4)); // File file =null;
personPostEntity.setPlayState((String) list2.get(5)); // try {
personPostEntity.setOrderState((String) list2.get(6)); // // 如果fileitem中封装的是普通输入项的数据
personPostEntity.setApplicantName((String) list2.get(7)); // if (item.isFormField()) {
personPostEntity.setSenderName((String) list2.get(8)); // String name = item.getFieldName();
personPostEntity.setSenderPhone((String) list2.get(9)); // // 解决普通输入项的数据的中文乱码问题
personPostEntity.setSenderAddress((String) list2.get(10)); // String value = item.getString("UTF-8");
personPostEntity.setRecipientName((String) list2.get(11)); // map.put(name, value);
personPostEntity.setRecipientPhone((String) list2.get(12)); // } else {
personPostEntity.setRecipientAddress((String) list2.get(13)); // // 如果fileitem中封装的是上传文件
personPostEntity.setOrderBlankNumber((String) list2.get(14)); // // 得到上传的文件名称,
personPostEntity.setGetToProvince((String) list2.get(15)); // filename = item.getName();
personPostEntity.setGetToCity((String) list2.get(16)); //
personPostEntity.setGetToCounty((String) list2.get(17)); // if (filename == null || filename.trim().equals("")) {
personPostEntity.setBusinessType((String) list2.get(18)); // continue;
personPostEntity.setLatticeMouthInformation((String) list2.get(19)); // }
personPostEntity.setNatureOfTheInternal((String) list2.get(20)); // InputStream in = item.getInputStream();
personPostEntity.setNatureOfTheInformation((String) list2.get(21)); // OutputStream os = new FileOutputStream(file);
personPostEntity.setFirstWhite((String) list2.get(22)); // int bytesRead = 0;
personPostEntity.setFileId(fileID); // byte[] buffer = new byte[8192];
entityList.add(personPostEntity); // 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);
// //保存EXCEL文件信息
// FileNameDicEntity fileNameDicEntity = new FileNameDicEntity();
// fileNameDicEntity.setFileName(filename);
// fileNameDicEntity.setUploadDate(new Date());
// //保存个人邮寄信息
// long fileID = fileNameDicService.saveFileNameDic(fileNameDicEntity);
// 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));
// personPostEntity.setFileId(fileID);
// 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;
// }
// }
} catch (Exception e) {
e.printStackTrace();
yxresp.outPutError("FileUploadException", "文件上载发生异常:" + e.getMessage());
} finally {
return yxresp.toJSONString();
} }
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;
} }
} }
} catch (FileUploadException e) { }catch (Exception e){
e.printStackTrace();
yxresp.outPutError("FileUploadException", "文件上载发生异常:" + e.getMessage()); }finally {
} finally {
return yxresp.toJSONString();
} }
return null;
} }
}
} \ No newline at end of file
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