上传文件

两种方式,一种是直接对上传的文件的流进行处理,然后直接存库;一种是将文件上传到一个远程服务器,然后将文件在远程服务器的地址返回。
1:代码
String filename=requestVo.getFilename();
String url=requestVo.getUrl();
URL urlParam = new URL(url);
// 打开链接
HttpURLConnection conn = (HttpURLConnection) urlParam.openConnection();
// 设置请求方式为”GET”
conn.setRequestMethod(“GET”);
// 超时响应时间为5秒
conn.setConnectTimeout(5 * 1000);
List vos = commissionRecordInfoService.uploadTransferOrderExcelData(conn.getInputStream(), filename);
public List uploadTransferOrderExcelData(InputStream in, String fileName) {
long starTime = System.currentTimeMillis();
List list = new ArrayList();// 订单
try {
Workbook wb = this.openWorkbook(in, fileName);
Sheet sheet = (Sheet) wb.getSheetAt(0);
list =this.HandleTransferOrderExcel(sheet,null);
} catch (Exception e) {
// logger.error(e);
throw new CustomException(0,e.getMessage(),null);
}
long endTime = System.currentTimeMillis();
long Time = endTime - starTime;
long mtime = Time / 1000;
// logger.info(“导入”
// + (list.size() + “条数据,用时” + String.valueOf(mtime) + “+秒”));
return list;
}
public List HandleTransferOrderExcel(Sheet sheet, Object object) throws Exception {
try {
Row row = null;
Cell cell = null;
int totalRows = sheet.getPhysicalNumberOfRows();
// String sheetName = sheet.getSheetName(); //获取sheet名字
List orderList = new ArrayList();// 业绩订单
for (int r = 1; r < totalRows; r++) // 行 去除标题行 故从1开始
{
row = sheet.getRow(r);
TransorderRecordVo orderExcel = new TransorderRecordVo();
if (row != null) {
for (int c = 0; c < 8; c++)// 列
{
cell = row.getCell(c);
String cellValue = “”;
if (null != cell) {
// 以下是判断数据的类型
switch (cell.getCellType()) {
case HSSFCell.CELL_TYPE_NUMERIC: // 数字
// 时间格式
if (HSSFDateUtil.isCellDateFormatted(cell)) {
Date dd = cell.getDateCellValue();
DateFormat df = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”);
cellValue = df.format(dd);
} else {
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cellValue = cell.getStringCellValue() + “”;
}
break;

                        case HSSFCell.CELL_TYPE_STRING: // 字符串
                            cellValue = cell.getStringCellValue();
                            break;

                        case HSSFCell.CELL_TYPE_BOOLEAN: // Boolean
                            cellValue = cell.getBooleanCellValue() + "";
                            break;

                        case HSSFCell.CELL_TYPE_FORMULA: // 公式
                            cellValue = cell.getCellFormula() + "";
                            break;

                        case HSSFCell.CELL_TYPE_BLANK: // 空值
                            cellValue = "";
                            break;

                        case HSSFCell.CELL_TYPE_ERROR: // 故障
                            cellValue = "";
                            break;
                        default:
                            cellValue = "";
                            break;
                        }
                    }
                    if (c == 0) { // 转单类型
                        if (cellValue.trim() == null || "".equals(cellValue.trim())) {
                            orderExcel = null;
                            break;
                        } else {
                            orderExcel.setTransOrderName(cellValue.trim());
                            if(TransferOrderReasonEnum.NUM.getDesc().equals(orderExcel.getTransOrderName())) {
                                orderExcel.setTransorderType(TransferOrderReasonEnum.NUM.getValue());
                            }else if(TransferOrderReasonEnum.PACKAGE.getDesc().equals(orderExcel.getTransOrderName())){
                                orderExcel.setTransorderType(TransferOrderReasonEnum.PACKAGE.getValue());
                            }
                        }
                    }

                    if (c == 1) { // 金融单号
                        if (cellValue.trim() != null && !"".equals(cellValue.trim())) {
                            orderExcel.setOrderId(cellValue.trim());
                            ;
                        } else {
                            // logger.info((r+1)+"行"+(c+1)+"列,);
                        }
                    }

                    if (c == 2) { // 转单人姓名
                        if (cellValue.trim() != null && !"".equals(cellValue.trim())) {
                             orderExcel.setTransferName(cellValue.trim());
                        } else {
                            // logger.info((r+1)+"行"+(c+1)+"列,);
                        }
                    }
                    if (c == 3) { // 转单人系统号
                        if (cellValue.trim() != null && !"".equals(cellValue.trim())) {
                             orderExcel.setTransferSystemid(cellValue.trim());
                        } else {
                            // logger.info((r+1)+"行"+(c+1)+"列,);
                        }
                    }
                    if (c == 4) { // 接单人姓名
                        if (cellValue.trim() != null && !"".equals(cellValue.trim())) {
                             orderExcel.setReceiverName(cellValue.trim());
                        } else {
                            // logger.info((r+1)+"行"+(c+1)+"列,);
                        }
                    }
                    if (c == 5) { // 接单人系统号
                        if (cellValue.trim() != null && !"".equals(cellValue.trim())) {
                             orderExcel.setReceiverSystemid(cellValue.trim());
                        } else {
                            // logger.info((r+1)+"行"+(c+1)+"列,);
                        }
                    }
                    if (c == 6) { // 转单原因
                        if (cellValue.trim() != null && !"".equals(cellValue.trim())) {
                             orderExcel.setTransorderReasonName(cellValue.trim());
                             orderExcel.setTransorderReason((TransferOrderReasonEnum.descOf(cellValue.trim()).getValue()));
                        } else {
                            // logger.info((r+1)+"行"+(c+1)+"列,);
                        }
                    }
                    if (c == 7) { // 转单时间
                        if (cellValue.trim() != null && !"".equals(cellValue.trim())) {
                            DateFormat df = new SimpleDateFormat("yyyyMMdd");
                             orderExcel.setTransferDate(df.parse(cellValue.trim()));
                        } else {
                            // logger.info((r+1)+"行"+(c+1)+"列,);
                        }
                    }
                }
                // 组装list
                if (orderExcel != null) {
                    // orderExcel.setReceviableCode();
                    orderList.add(orderExcel);
                }
            }
        }
        return orderList;

    } catch (Exception e) {
        throw e;
    }

}

2:直接上传文件 那么就是直接从文件流中获取数据,直接进行处理
String filename=requestVo.getFilename();
String url=requestVo.getUrl();
URL urlParam = new URL(url);
// 打开链接
HttpURLConnection conn = (HttpURLConnection) urlParam.openConnection();
// 设置请求方式为”GET”
conn.setRequestMethod(“GET”);
// 超时响应时间为5秒
conn.setConnectTimeout(5 * 1000);
// 加载excel
List list = new ArrayList();// 订单
String excelDatas = ParseExcelUtil.getInstance().loadExcel(filename, conn.getInputStream(),
list);
System.out.println(url);
request.setCharacterEncoding(“UTF-8”);
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
MultipartFile file = multipartRequest.getFile(“uploadFile”);
InputStream inputStream = file.getInputStream();
String s = file.getOriginalFilename();

你可能感兴趣的:(文件上传)