Commit 9c472ca9 authored by dahai's avatar dahai

修改上传EXCEL文件

parent f85f3ad6
......@@ -22,7 +22,8 @@ import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.Consumes;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.io.*;
import java.io.File;
import java.io.IOException;
import java.sql.Time;
import java.util.*;
......@@ -78,25 +79,25 @@ public class ReadExcelApi {
// 如果fileitem中封装的是上传文件
// 得到上传的文件名称,
filename = item.getName();
File file = null;
try {
// 如果fileitem中封装的是普通输入项的数据
// 如果fileitem中封装的是上传文件
// 得到上传的文件名称,
filename = item.getName();
if (filename == null || filename.trim().equals("")) {
continue;
System.out.println(item.getName()); // 为上传的key值
System.out.println(item.getOriginalFilename()); // 上传的真实文件的文件名
File file = null;
try {
file = File.createTempFile("prefix", "_" + item.getOriginalFilename());
item.transferTo(file);
} catch (IOException e) {
e.printStackTrace();
}
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();
// CommonsMultipartFile cf= (CommonsMultipartFile)item;
// DiskFileItem fi = (DiskFileItem)cf.getFileItem();
// File file = fi.getStoreLocation();
ReadExcel readExcel = new ReadExcel();
List list1 = readExcel.readExcel(file);
//保存EXCEL文件信息
......@@ -136,20 +137,12 @@ public class ReadExcelApi {
personPostEntity.setNatureOfTheInternal((String) list2.get(20));
personPostEntity.setNatureOfTheInformation((String) list2.get(21));
personPostEntity.setFirstWhite((String) list2.get(22));
personPostEntity.setFileId(fileID);
personPostEntity.setFileId(fileNameDicEntity.getFileId());
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);
......
......@@ -4,7 +4,9 @@ import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import java.io.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
......@@ -14,12 +16,12 @@ import java.util.List;
public class ReadExcel {
// 去读Excel的方法readExcel,该方法的入口参数为一个File对象
public List readExcel(File file) {
public List readExcel(File file) {
try {
// 创建输入流,读取Excel
InputStream is = new FileInputStream(file.getAbsolutePath());
// // 创建输入流,读取Excel
// InputStream is = new FileInputStream(file.getAbsolutePath());
// jxl提供的Workbook类
Workbook wb = Workbook.getWorkbook(is);
Workbook wb = Workbook.getWorkbook(file);
// Excel的页签数量
int sheet_size = wb.getNumberOfSheets();
for (int index = 0; index < sheet_size; index++) {
......
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