PHP发送文件到JAVA项目
https://blog.csdn.net/u012685554/article/details/126995307
亲测有用。
php代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | $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代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 | @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 ; } } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架