1. 程式人生 > >上傳附件獲取不到路徑值。

上傳附件獲取不到路徑值。

上傳附件時應該吧form表單的型別設定為enctype="multipart/form-data"

java程式碼

int updateSize=0;
    Map<String, Object> result = new HashMap();
    String path="";
        DiskFileItemFactory factory = new DiskFileItemFactory();
        factory.setSizeThreshold(Integer.parseInt(ReadConfigUtil.getPlatformConfig("fileUpload.memorySizeThreshold")));
         ServletFileUpload upload = new ServletFileUpload(factory);
       upload.setSizeMax(Integer.parseInt(ReadConfigUtil.getPlatformConfig("fileUpload.totalFileMaxsize")));
        upload.setHeaderEncoding("UTF-8");
      HttpSession session = request.getSession();
         
       String checkResutlMessage = null;
       String errorCode = null;
        int totalCount = 0;
        int validatedDataCount = 0;
      try {
         List<?> items = upload.parseRequest(request);
         FileItem item = null;
        for (int i = 0; i < items.size(); i++) {
            item = (FileItem)items.get(i);
       if ((!item.isFormField()) && (item.getName().length() > 0)) {
    String a=item.toString();
    path=a.substring(a.indexOf("StoreLocation=")+14,a.indexOf("size")-2);
               Workbook workbook = ExcelParseUtil.getWorkbook(item);
       Sheet dataSheet = workbook.getSheetAt(0);
        Cell cell = null;
        for (int j = 1; j <= dataSheet.getLastRowNum(); j++) {
Row row = dataSheet.getRow(j);
if (row == null) {
continue;
}
// 遍歷獲取該行的所有資料
String ext_field1 = "";
if (row.getCell(0) != null) {
row.getCell(0).setCellType(cell.CELL_TYPE_STRING);
cell = row.getCell(0);
ext_field1 = cell.getStringCellValue();
}
String fileno = "";
if (row.getCell(1) != null) {
row.getCell(1).setCellType(cell.CELL_TYPE_STRING);
cell = row.getCell(1);
fileno = cell.getStringCellValue();
}
String ext_field2 = "";
if (row.getCell(2) != null) {
row.getCell(2).setCellType(cell.CELL_TYPE_STRING);
cell = row.getCell(2);
ext_field2 = cell.getStringCellValue();
}
String ext_field3 = "";
if (row.getCell(3) != null) {
row.getCell(3).setCellType(cell.CELL_TYPE_STRING);
cell = row.getCell(3);
ext_field3 = cell.getStringCellValue();
}
String keyword = "";
if (row.getCell(4) != null) {
row.getCell(4).setCellType(cell.CELL_TYPE_STRING);
cell = row.getCell(4);
keyword = cell.getStringCellValue();
}
String filename = "";
if (row.getCell(5) != null) {
row.getCell(5).setCellType(cell.CELL_TYPE_STRING);
cell = row.getCell(5);
filename = cell.getStringCellValue().replace("'", "''");
}
String filetype = "";
if (row.getCell(6) != null) {
row.getCell(6).setCellType(cell.CELL_TYPE_STRING);
cell = row.getCell(6);
filetype = cell.getStringCellValue();
}
String createusername = "";
if (row.getCell(7) != null) {
row.getCell(7).setCellType(cell.CELL_TYPE_STRING);
cell = row.getCell(7);
createusername = cell.getStringCellValue();
}
String versionupdatetime = "";
if (row.getCell(8) != null) {
row.getCell(8).setCellType(cell.CELL_TYPE_STRING);
cell = row.getCell(8);
versionupdatetime = cell.getStringCellValue();
// 如果值為空則插入當前時間
if ("".equals(versionupdatetime)) {
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
versionupdatetime = sdf.format(System.currentTimeMillis());
}
} else {
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
versionupdatetime = sdf.format(System.currentTimeMillis());
}
long size = 0;
if (row.getCell(9) != null) {
row.getCell(9).setCellType(cell.CELL_TYPE_STRING);
cell = row.getCell(9);
String filesize = cell.getStringCellValue();
filesize = filesize.replace("k", "");
size = (long) ((Double.valueOf(filesize)) * 1024);
}
String filelocation = "";
if (row.getCell(10) != null) {
row.getCell(10).setCellType(cell.CELL_TYPE_STRING);
cell = row.getCell(10);
filelocation = cell.getStringCellValue();
}
String eventid = "";
if (row.getCell(11) != null) {
row.getCell(11).setCellType(cell.CELL_TYPE_STRING);
cell = row.getCell(11);
eventid = cell.getStringCellValue();
}
updateSize=this.docCenterService.getAllPath(ext_field1,fileno,ext_field2,ext_field3,keyword,filename,filetype,createusername,versionupdatetime,filelocation,size,eventid);
          }
         }
    }
      } catch (Exception e) {
        e.printStackTrace();
      }
    Map map=new HashMap<>();
    if(updateSize!=0){
    map.put("success", 1);
    map.put("size", updateSize);
    }
    return map;