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,18 +63,25 @@ public class ReadExcelApi { ...@@ -59,18 +63,25 @@ 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 (多部分解析器)
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(
requ.getSession().getServletContext());
// 判断是否是多数据段提交格式
if (multipartResolver.isMultipart(requ)) {
MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) requ;
Iterator<String> iter = multiRequest.getFileNames();
while (iter.hasNext()) {
MultipartFile item = multiRequest.getFile(iter.next());
try {
// 如果fileitem中封装的是普通输入项的数据
// 如果fileitem中封装的是上传文件
// 得到上传的文件名称,
filename = item.getName();
File file = null;
try { try {
// 如果fileitem中封装的是普通输入项的数据 // 如果fileitem中封装的是普通输入项的数据
if (item.isFormField()) {
String name = item.getFieldName();
// 解决普通输入项的数据的中文乱码问题
String value = item.getString("UTF-8");
map.put(name, value);
} else {
// 如果fileitem中封装的是上传文件 // 如果fileitem中封装的是上传文件
// 得到上传的文件名称, // 得到上传的文件名称,
filename = item.getName(); filename = item.getName();
...@@ -87,7 +98,6 @@ public class ReadExcelApi { ...@@ -87,7 +98,6 @@ public class ReadExcelApi {
} }
os.close(); os.close();
in.close(); in.close();
}
ReadExcel readExcel = new ReadExcel(); ReadExcel readExcel = new ReadExcel();
List list1 = readExcel.readExcel(file); List list1 = readExcel.readExcel(file);
//保存EXCEL文件信息 //保存EXCEL文件信息
...@@ -96,12 +106,12 @@ public class ReadExcelApi { ...@@ -96,12 +106,12 @@ public class ReadExcelApi {
fileNameDicEntity.setUploadDate(new Date()); fileNameDicEntity.setUploadDate(new Date());
//保存个人邮寄信息 //保存个人邮寄信息
long fileID = fileNameDicService.saveFileNameDic(fileNameDicEntity); long fileID = fileNameDicService.saveFileNameDic(fileNameDicEntity);
List<PersonPostEntity> entityList= new ArrayList<PersonPostEntity>(); List<PersonPostEntity> entityList = new ArrayList<PersonPostEntity>();
for (int i = 0; i < list1.size(); i++){ for (int i = 0; i < list1.size(); i++) {
PersonPostEntity personPostEntity = new PersonPostEntity(); PersonPostEntity personPostEntity = new PersonPostEntity();
List list2 = (List) list1.get(i); List list2 = (List) list1.get(i);
//跳过空行和第一行 //跳过空行和第一行
if (list2 == null||i==0){ if (list2 == null || i == 0) {
continue; continue;
} }
personPostEntity.setBackWaybillNumber((String) list2.get(0)); personPostEntity.setBackWaybillNumber((String) list2.get(0));
...@@ -131,6 +141,8 @@ public class ReadExcelApi { ...@@ -131,6 +141,8 @@ public class ReadExcelApi {
entityList.add(personPostEntity); entityList.add(personPostEntity);
} }
boolean b = personPostService.savePersonPost(entityList); boolean b = personPostService.savePersonPost(entityList);
} catch (UnsupportedEncodingException e) { } catch (UnsupportedEncodingException e) {
e.printStackTrace(); e.printStackTrace();
yxresp.outPutError("UnsupportedEncodingException", "上传文件时发现文件编码错误:" + e.getMessage()); yxresp.outPutError("UnsupportedEncodingException", "上传文件时发现文件编码错误:" + e.getMessage());
...@@ -143,14 +155,109 @@ public class ReadExcelApi { ...@@ -143,14 +155,109 @@ public class ReadExcelApi {
e.printStackTrace(); e.printStackTrace();
yxresp.outPutError("Exception", "上传文件时发生错误,非法XML文件:" + filename); yxresp.outPutError("Exception", "上传文件时发生错误,非法XML文件:" + filename);
continue; continue;
} finally {
} }
}
} catch (FileUploadException e) { // 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);
// //保存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(); e.printStackTrace();
yxresp.outPutError("FileUploadException", "文件上载发生异常:" + e.getMessage()); yxresp.outPutError("FileUploadException", "文件上载发生异常:" + e.getMessage());
} finally { } finally {
return yxresp.toJSONString(); return yxresp.toJSONString();
} }
} }
}
}catch (Exception e){
}finally {
}
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