java后台調用文件上傳接口


借鑒:https://blog.csdn.net/yjclsx/article/details/70675057

 1 /**
 2      * 調用流程上傳文件接口上傳文件
 3      * @param url
 4      * @param path
 5      * @return
 6      */
 7     public static String sendPostUplodFile(String url,String path) {
 8         DataOutputStream out = null;
 9         BufferedReader in = null;
10         String result = "";
11         try {
12             URL realUrl = new URL(url);
13             //打開和URL之間的連接
14             HttpURLConnection conn = (HttpURLConnection)realUrl.openConnection();
15             //發送POST請求必須設置如下兩行
16             conn.setDoOutput(true);
17             conn.setDoInput(true);
18             
19             String BOUNDARY = "----WebKitFormBoundary07I8UIuBx6LN2KyY";
20             conn.setUseCaches(false);
21             conn.setRequestMethod("POST");
22             conn.setRequestProperty("connection", "Keep-Alive");
23 //            conn.setRequestProperty("user-agent", "Mozilla/4.0 (conpatible; MSIE 6.0; Windows NT 5.1; SV1)");
24             conn.setRequestProperty("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36");
25             conn.setRequestProperty("Charsert", "UTF-8");
26             conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + BOUNDARY);
27             conn.connect();
28             
29             out = new DataOutputStream(conn.getOutputStream());
30             byte[] end_data = ("\r\n--" + BOUNDARY + "--\r\n").getBytes();
31             //添加參數
32             StringBuffer sb1 = new StringBuffer();
33             sb1.append("--");
34             sb1.append(BOUNDARY);
35             sb1.append("\r\n");
36             sb1.append("Content-Disposition: form-data;name=\"luid\"");
37             sb1.append("\r\n");
38             sb1.append("\r\n");
39             sb1.append("123");
40             sb1.append("\r\n");
41             out.write(sb1.toString().getBytes());
42             //添加參數file
43             File file = new File(path);
44             StringBuffer sb = new StringBuffer();
45             sb.append("--");
46             sb.append(BOUNDARY);
47             sb.append("\r\n");
48             sb.append("Content-Disposition: form-data;name=\"file\";filename=\"" + file.getName() + "\"");
49             sb.append("\r\n");
50             sb.append("Content-Type: application/octet-stream");
51             sb.append("\r\n");
52             sb.append("\r\n");
53             out.write(sb.toString().getBytes());
54             
55             DataInputStream in1 = new DataInputStream(new FileInputStream(file));
56             int bytes = 0;
57             byte[] bufferOut = new byte[1024];
58             while ((bytes = in1.read(bufferOut)) != -1) {
59                 out.write(bufferOut,0,bytes);
60             }
61             out.write("\r\n".getBytes());
62             in1.close();
63             out.write(end_data);
64             
65             //flush輸出流的緩沖
66             out.flush();
67             //定義BufferedReader輸入流來讀取URL的響應
68             in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
69             String line;
70             while ((line = in.readLine()) != null) {
71                 result += line;
72             }
73         } catch (Exception e) {
74             // TODO Auto-generated catch block
75             log.error("發送POST請求出現異常" + e);
76             e.printStackTrace();
77         }finally {
78             try {
79                 if (out != null) {
80                     out.close();
81                 }
82                 if (in != null) {
83                     in.close();
84                 }
85             } catch (Exception ex) {
86                 // TODO: handle exception
87                 ex.printStackTrace();
88             }
89         }
90         return result;
91     }


 //將字符串轉換json並取值
1
public static String getFileId(String result) { 2 JSONObject json = JSONObject.parseObject(result); 3 String fileId = ""; 4 if (result != null) { 5 String code = json.getString("errorCode"); 6 if (code.equals("0000")) { 7 fileId = json.getString("fileId"); 8 } 9 } 10 11 return fileId; 12 }

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM