PHP发送文件到JAVA项目
https://blog.csdn.net/u012685554/article/details/126995307
亲测有用。
php代码
$file = $_FILES['order_upload']['tmp_name']; // // var_dump($file);exit(); // // 判断文件是否存在 if (!file_exists($file)) { $this->show('文件不存在!'); exit; } $params = [ 'file'=>new \CURLFILE($file), 'id'=>'1', 'fileName'=>$_FILES['order_upload']['name'], ]; $url = "http://localhost:8080/upload"; $header = array('Content-Type: multipart/form-data'); $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");//3.请求方式 curl_setopt($curl, CURLOPT_POSTFIELDS, $params); curl_setopt($curl, CURLOPT_HEADER, 0); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($curl,CURLOPT_HEADER,$header); $response = curl_exec($curl); echo $response;
java代码
@RestController @Slf4j public class TestController { @PostMapping("/upload") @ResponseBody public String uploadOrderExcel(@RequestParam("file") MultipartFile file,@RequestParam("id")String id,@RequestParam("fileName")String fileName) { System.out.println("fileName:" + file.getOriginalFilename()); System.out.println("id:" + id); System.out.println("fileName:" + fileName); String lastName = Objects.requireNonNull(fileName.substring(fileName.lastIndexOf(".") + 1)); switch (lastName) { case "xlsx": try { //xlsx InputStream inputStream = file.getInputStream(); XSSFWorkbook wb = new XSSFWorkbook(inputStream); XSSFSheet sheet = wb.getSheetAt(0); //获取总行数 index从0开始 int rowNum = sheet.getLastRowNum(); //获取总列数,index好像从1开始 int colNum = sheet.getRow(0).getPhysicalNumberOfCells(); List<String> colNames = new ArrayList<>(); //插入列名 XSSFRow row = sheet.getRow(0); for (int i = 0; i < colNum; i++) { colNames.add(String.valueOf(row.getCell(i))); } Map<String, Integer> map = checkDataFromFile(colNames); for (Map.Entry<String, Integer> entry : map.entrySet()) { System.out.print(entry.getKey() + " "); } System.out.println(); for (int i = 1; i <= rowNum; i++) { for (Map.Entry<String, Integer> entry : map.entrySet()) { XSSFRow nowRow = sheet.getRow(i); System.out.print(nowRow.getCell(entry.getValue()) + " "); } System.out.println(); } return "rowNum: " + rowNum + " colNum: " + colNum; } catch (Exception e) { throw new RuntimeException(e); } case "xls": //xls try { InputStream inputStream = file.getInputStream(); // 创建workbook对象,读取整个文档 HSSFWorkbook wb = new HSSFWorkbook(inputStream); HSSFSheet sheet = wb.getSheetAt(0); //获取行数,列数 int rowNum = sheet.getLastRowNum(); int colNum = sheet.getRow(0).getPhysicalNumberOfCells(); List<String> colNames = new ArrayList<>(); //插入列名 HSSFRow row = sheet.getRow(0); for (int i = 0; i < colNum; i++) { colNames.add(String.valueOf(row.getCell(i))); } Map<String, Integer> map = checkDataFromFile(colNames); for (Map.Entry<String, Integer> entry : map.entrySet()) { System.out.print(entry.getKey() + " "); } System.out.println(); for (int i = 1; i <= rowNum; i++) { for (Map.Entry<String, Integer> entry : map.entrySet()) { HSSFRow nowRow = sheet.getRow(i); System.out.print(nowRow.getCell(entry.getValue()) + " "); } System.out.println(); } return "rowNum: " + rowNum + " colNum: " + colNum; } catch (Exception e) { throw new RuntimeException(e); } case "csv": return "csv"; default: return "null"; } } public Map<String,Integer> checkDataFromFile(List<String> colName) { //检查所需字段是否均存在表格内,存在返回Map<字段名,列索引>,不存在返回null //这里可以抽离出去封装 Map<String, List<String>> oMap = new HashMap<>(16); List<String> list1 = new ArrayList<>(); list1.add("收货人姓名"); list1.add("收件人"); oMap.put("name", list1); List<String> list2 = new ArrayList<>(); list2.add("收货人地址"); list2.add("收货地址"); list2.add("地址"); oMap.put("address", list2); List<String> list3 = new ArrayList<>(); list3.add("收货人姓名电话"); list3.add("联系方式"); list3.add("手机"); oMap.put("phone", list3); List<String> list4 = new ArrayList<>(); list4.add("商品标题"); list4.add("商品名称"); list4.add("型号"); oMap.put("goodsName", list4); List<String> list5 = new ArrayList<>(); list5.add("数量"); list5.add("商品数量"); oMap.put("goodsNum", list5); boolean can = true; Map<String, Integer> resultMap = new HashMap<>(16); for (Map.Entry<String, List<String>> entry : oMap.entrySet()) { boolean found = false; for (String o : entry.getValue()) { for (int i = 0; i < colName.size(); i++) { if (colName.get(i).equals(o)) { resultMap.put(String.valueOf(entry.getKey()), i); found = true; break; } } } //如果有哪个字段没有匹配上 if (!found) { can = false; break; } } //如果全部匹配 if (can) { return resultMap; } //默认返回 return null; } }