日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) >

JAVA模拟HTTP post请求上传图片

發(fā)布時(shí)間:2025/3/20 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 JAVA模拟HTTP post请求上传图片 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

2019獨(dú)角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>

1.原理:實(shí)現(xiàn)思路和具體步驟就是模擬頁(yè)面的請(qǐng)求

?

/*** @author qimh* @description 模擬form表單,上傳圖片* @param fileName -- 圖片路徑* @return 接口返回的json數(shù)據(jù)* 原理:模擬form表單提交:把請(qǐng)求頭部信息和和img 信息 寫入到輸出流中,* 通過流把img寫入到服務(wù)器臨時(shí)目錄里,然后服務(wù)器再把img移到指定的位置* 最后通過寫入流來(lái)獲取post的響應(yīng)信息。* */public static void uploadImg(String fileName) { try { // 換行符 final String newLine = "\r\n"; final String boundaryPrefix = "--"; // 定義數(shù)據(jù)分隔線 String BOUNDARY = "========7d4a6d158c9"; // 服務(wù)器的域名 URL url = new URL(ECSheng);//"www.myhost.com",ECSheng=http://192.168.1.59/ecstore/index.php/openapi/syncnovo/image_upload ,即為form的action值HttpURLConnection conn = (HttpURLConnection) url.openConnection(); // 設(shè)置為POST情 conn.setRequestMethod("POST"); // 發(fā)送POST請(qǐng)求必須設(shè)置如下兩行 conn.setDoOutput(true); conn.setDoInput(true); conn.setUseCaches(false); // 設(shè)置請(qǐng)求頭參數(shù) conn.setRequestProperty("connection", "Keep-Alive"); conn.setRequestProperty("Charsert", "UTF-8"); conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + BOUNDARY); OutputStream out = new DataOutputStream(conn.getOutputStream()); // 上傳文件 File file = new File(fileName); StringBuilder sb = new StringBuilder(); sb.append(boundaryPrefix); sb.append(BOUNDARY); sb.append(newLine); // 文件參數(shù),photo參數(shù)名可以隨意修改 sb.append("Content-Disposition: form-data;name=\"upload_item\";filename=\"" + fileName + "\"" + newLine); sb.append("Content-Type:image/jpeg"); // 參數(shù)頭設(shè)置完以后需要兩個(gè)換行,然后才是參數(shù)內(nèi)容 sb.append(newLine); sb.append(newLine); // 將參數(shù)頭的數(shù)據(jù)寫入到輸出流中 out.write(sb.toString().getBytes()); // 數(shù)據(jù)輸入流,用于讀取文件數(shù)據(jù) DataInputStream in = new DataInputStream(new FileInputStream(file)); byte[] bufferOut = new byte[1024]; int bytes = 0; // 每次讀1KB數(shù)據(jù),并且將文件數(shù)據(jù)寫入到輸出流中 while ((bytes = in.read(bufferOut)) != -1) { out.write(bufferOut, 0, bytes); } // 最后添加換行 out.write(newLine.getBytes()); in.close(); // 定義最后數(shù)據(jù)分隔線,即--加上BOUNDARY再加上--。 byte[] end_data = (newLine + boundaryPrefix + BOUNDARY + boundaryPrefix + newLine) .getBytes(); // 寫上結(jié)尾標(biāo)識(shí) out.write(end_data); out.flush(); out.close(); // 定義BufferedReader輸入流來(lái)讀取URL的響應(yīng) ----讀取返回的結(jié)果BufferedReader reader = new BufferedReader(new InputStreamReader( conn.getInputStream())); String line = null; while ((line = reader.readLine()) != null) { // System.out.println(line); JSONObject jsonObject = new JSONObject(line);//創(chuàng)建jsonObjec對(duì)象String json = jsonObject.toString();//josn格式的字符串System.out.println(json); } } catch (Exception e) { System.out.println("發(fā)送POST請(qǐng)求出現(xiàn)異常!" + e); e.printStackTrace(); } }

參考鏈接:http://www.cnblogs.com/woolhc/p/6123975.html
? ? ? ? ? ? ? ??

????????????????http://blog.csdn.net/yjclsx/article/details/70675057

轉(zhuǎn)載于:https://my.oschina.net/qimhkaiyuan/blog/1358229

總結(jié)

以上是生活随笔為你收集整理的JAVA模拟HTTP post请求上传图片的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。