當前位置:首頁 » 編程語言 » http解析java

http解析java

發布時間: 2022-08-31 17:38:39

① 如何實現java解析網路協議報文

普通參數:
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

文件參數:
Content-Type: application/octet-stream
Content-Transfer-Encoding: binary

參數實體的最後一行是: --加上boundary加上--,最後換行,這里的 格式即為: --OCqxMF6-JxtxoMDHmoG5W5eY9MGRsTBp--。

模擬文件上傳請求
public static void uploadFile(String fileName) {
try {
// 換行符
final String newLine = "\r\n";
final String boundaryPrefix = "--";
// 定義數據分隔線
String BOUNDARY = "========7d4a6d158c9";
// 伺服器的域名
URL url = new URL("www.myhost.com");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
// 設置為POST情
conn.setRequestMethod("POST");
// 發送POST請求必須設置如下兩行
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches(false);
// 設置請求頭參數
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);
// 文件參數,photo參數名可以隨意修改
sb.append("Content-Disposition: form-data;name=\"photo\";filename=\"" + fileName
+ "\"" + newLine);
sb.append("Content-Type:application/octet-stream");
// 參數頭設置完以後需要兩個換行,然後才是參數內容
sb.append(newLine);
sb.append(newLine);
// 將參數頭的數據寫入到輸出流中
out.write(sb.toString().getBytes());
// 數據輸入流,用於讀取文件數據
DataInputStream in = new DataInputStream(new FileInputStream(
file));
byte[] bufferOut = new byte[1024];
int bytes = 0;
// 每次讀1KB數據,並且將文件數據寫入到輸出流中
while ((bytes = in.read(bufferOut)) != -1) {
out.write(bufferOut, 0, bytes);
}
// 最後添加換行
out.write(newLine.getBytes());
in.close();
// 定義最後數據分隔線,即--加上BOUNDARY再加上--。
byte[] end_data = (newLine + boundaryPrefix + BOUNDARY + boundaryPrefix + newLine)
.getBytes();
// 寫上結尾標識
out.write(end_data);
out.flush();
out.close();
// 定義BufferedReader輸入流來讀取URL的響應
// BufferedReader reader = new BufferedReader(new InputStreamReader(
// conn.getInputStream()));
// String line = null;
// while ((line = reader.readLine()) != null) {
// System.out.println(line);
// }
} catch (Exception e) {
System.out.println("發送POST請求出現異常!" + e);
e.printStackTrace();
}
}

② java 怎麼把一個從http返回的json 和xml 文件 解析出來取得他具體的值!

定義模板,然後在類中聲明,集成spring後就可以通過屬性注入了,很方便

③ 求解Java不用組件或框架解析http請求上傳圖片

首先這個問題很多,bufferedReader不能讀二進制文件,你不用框架就是作死。

④ javaweb HttpServletRequest 無法解析為類型

原因是缺少Servlet的jar包,添加運行環境就好了,步驟如下:

1、web程序工程名上右鍵-->properties(屬性)-->JAVA構建路徑-->庫-->添加庫-->選擇server runtime

完成之後 重復第一步 就OK了

⑤ java解析html是jsoup還是htmlparse還是其他的什麼

用jsoup解析html或者htmlparse,不過比較難用,jsoup是jquery語法比較方便。

⑥ java怎麼解析http post請求數據

一般返回的是json格式,用阿里的fast json第三方包來解析。

⑦ java解析http上的xml 根據國家和城市的不同要發送200多次請求的地址然後在解析 但是解析一般就停 不解析了

是否可以先把文件讀出來,再解析。你的是一邊讀一邊解析

⑧ JAVA裡面的HTTP是什麼

java本身不提供http功能。

http是一個應用層協議,底層用到了TCP。Java提供了TCP協議,但是沒有http的實現。

但是,可以在網上找到開源的http client/server實現。例如apache-common-http之類的包。

⑨ java 解析http請求數據

string p1=request.getParameter("p1");

string p2=request.getParameter("p2");
這樣就獲取到數據了,然後你就可以存進資料庫中或者進行數據處理。

熱點內容
加密殼sdk 發布:2025-05-12 07:38:29 瀏覽:509
電腦網線通伺服器 發布:2025-05-12 07:34:59 瀏覽:680
訪問法概念 發布:2025-05-12 07:27:14 瀏覽:406
遺傳演算法例子 發布:2025-05-12 07:27:11 瀏覽:266
matlab語言編程 發布:2025-05-12 07:05:16 瀏覽:482
解壓油畫棒 發布:2025-05-12 06:56:56 瀏覽:716
如何安裝語言編譯器 發布:2025-05-12 06:55:05 瀏覽:300
c語言程序設計題目 發布:2025-05-12 06:46:46 瀏覽:712
虛擬機上傳文件 發布:2025-05-12 06:41:52 瀏覽:572
編程模特 發布:2025-05-12 06:41:51 瀏覽:271