日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

Java利用HttpURLConnection發(fā)送post請(qǐng)求上傳文件

 時(shí)間要去哪 2014-07-19
  在頁(yè)面里實(shí)現(xiàn)上傳文件不是什么難事,寫個(gè)form,加上enctype = "multipart/form-data",在寫個(gè)接收的就可以了,沒(méi)什么難的,如果要用java.net.HttpURLConnection來(lái)實(shí)現(xiàn)文件上傳,還真有點(diǎn)搞頭.:-)

  1.先寫個(gè)servlet把接收到的 HTTP 信息保存在一個(gè)文件中, 看一下 form 表單到底封裝了什么樣的信息。

  Java代碼

  public void doPost(HttpServletRequest request, HttpServletResponse response)

  throws ServletException, IOException {

  //獲取輸入流,是HTTP協(xié)議中的實(shí)體內(nèi)容

  ServletInputStream  in=request.getInputStream();

  //緩沖區(qū)

  byte buffer[]=new byte[1024];

  FileOutputStream out=new FileOutputStream("d:\\test.log");

  int len=sis.read(buffer, 0, 1024);

  //把流里的信息循環(huán)讀入到file.log文件中

  while( len!=-1 ){

  out.write(buffer, 0, len);

  len=in.readLine(buffer, 0, 1024);

  }

  out.close();

  in.close();

  }

  來(lái)一個(gè)form表單。

  <form name="upform" action="upload.do" method="POST"

  enctype="multipart/form-data">

  參數(shù)<input type="text" name="username"/><br/>

  文件1<input type="file" name="file1"/><br/>

  文件2<input type="file" name="file2"/><br/>

  <input type="submit" value="Submit" />

  <br />

  </form>

  假如我參數(shù)寫的內(nèi)容是hello word,然后二個(gè)文件是二個(gè)簡(jiǎn)單的txt文件,上傳后test.log里如下

  -----------------------------7da2e536604c8

  Content-Disposition: form-data; name="username"

  hello word

  -----------------------------7da2e536604c8

  Content-Disposition: form-data; name="file1"; filename="D:\haha.txt"

  Content-Type: text/plain

  haha

  hahaha

  -----------------------------7da2e536604c8

  Content-Disposition: form-data; name="file2"; filename="D:\huhu.txt"

  Content-Type: text/plain

  messi

  huhu

  -----------------------------7da2e536604c8--

  研究下規(guī)律發(fā)現(xiàn)有如下幾點(diǎn)特征

  1.第一行是“ -----------------------------7d92221b604bc ”作為分隔符,然后是“ \r\n ” 回車換行符。 這個(gè)7d92221b604bc 分隔符瀏覽器是隨機(jī)生成的。

  2.第二行是Content-Disposition: form-data; name="file2"; filename="D:\huhu.txt";name=對(duì)應(yīng)input的name值,filename對(duì)應(yīng)要上傳的文件名(包括路徑在內(nèi)),

  3.第三行如果是文件就有Content-Type: text/plain;這里上傳的是txt文件所以是text/plain,如果上穿的是jpg圖片的話就是image/jpg了,可以自己試試看看。

  然后就是回車換行符。

  4.在下就是文件或參數(shù)的內(nèi)容或值了。如:hello word。

  5.最后一行是-----------------------------7da2e536604c8--,注意最后多了二個(gè)--;

  有了這些就可以使用HttpURLConnection來(lái)實(shí)現(xiàn)上傳文件功能了

  Java代碼 public void upload(){

  List<String> list  = new ArrayList<String>();  //要上傳的文件名,如:d:\haha.doc.你要實(shí)現(xiàn)自己的業(yè)務(wù)。我這里就是一個(gè)空l(shuí)ist.

  try {

  String BOUNDARY = "---------7d4a6d158c9"; // 定義數(shù)據(jù)分隔線

  URL url = new URL("http://localhost/JobPro/upload.do");

  HttpURLConnection conn = (HttpURLConnection) url.openConnection();

  // 發(fā)送POST請(qǐng)求必須設(shè)置如下兩行

  conn.setDoOutput(true);

  conn.setDoInput(true);

  conn.setUseCaches(false);

  conn.setRequestMethod("POST");

  conn.setRequestProperty("connection", "Keep-Alive");

  conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");

  conn.setRequestProperty("Charsert", "UTF-8");

  conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + BOUNDARY);

  OutputStream out = new DataOutputStream(conn.getOutputStream());

  byte[] end_data = ("\r\n--" + BOUNDARY + "--\r\n").getBytes();// 定義最后數(shù)據(jù)分隔線

  int leng = list.size();

  for(int i=0;i<leng;i++){

  String fname = list.get(i);

  File file = new File(fname);

  StringBuilder sb = new StringBuilder();

  sb.append("--");

  sb.append(BOUNDARY);

  sb.append("\r\n");

  sb.append("Content-Disposition: form-data;name=\"file"+i+"\";filename=\""+ file.getName() + "\"\r\n");

  sb.append("Content-Type:application/octet-stream\r\n\r\n");

  byte[] data = sb.toString().getBytes();

  out.write(data);

  DataInputStream in = new DataInputStream(new FileInputStream(file));

  int bytes = 0;

  byte[] bufferOut = new byte[1024];

  while ((bytes = in.read(bufferOut)) != -1) {

  out.write(bufferOut, 0, bytes);

  }

  out.write("\r\n".getBytes()); //多個(gè)文件時(shí),二個(gè)文件之間加入這個(gè)

  in.close();

  }

  out.write(end_data);

  out.flush();

  out.close();

  // 定義BufferedReader輸入流來(lái)讀取URL的響應(yīng)

  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("發(fā)送POST請(qǐng)求出現(xiàn)異常!" + e);

  e.printStackTrace();

  }

  }


    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多