自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

HttpURLConnection上傳文件

開發(fā) 開發(fā)工具
博主發(fā)表的文章,有的是自己原創(chuàng),有的是這些年本人從網(wǎng)上積累的,方便大家學(xué)習(xí)。

[[178962]]

  1. import java.io.BufferedReader; 
  2. import java.io.DataInputStream; 
  3. import java.io.DataOutputStream; 
  4. import java.io.File; 
  5. import java.io.FileInputStream; 
  6. import java.io.InputStreamReader; 
  7. import java.io.OutputStream; 
  8. import java.net.URL; 
  9.  
  10. import sun.net.www.protocol.http.HttpURLConnection; 
  11. /** 
  12.  * 主要實(shí)現(xiàn)文件上傳,和接收方綁定接收后信息導(dǎo)入?yún)?shù)傳遞 
  13.  * @author zyb 
  14.  * 
  15.  */ 
  16. public class HttpURLConnectionServices { 
  17.  /** 
  18.  * @param fileName 要上傳的文件,列:e:/upload/SSD4k對(duì)齊分區(qū).zip 
  19.  * @param Url 上傳路徑端口號(hào)和項(xiàng)目名稱,列:http://192.168.1.209:9080/gjbmj 
  20.  * @param strSiteID 對(duì)方的站點(diǎn)編號(hào) 
  21.  * @param strColumnID 對(duì)方的欄目編號(hào) 
  22.  * @param strDespatcher 發(fā)送信息人 
  23.  * @param strMechanism 發(fā)送信息機(jī)構(gòu) 
  24.  * @param strOther1 
  25.  */ 
  26.  public static void post(String fileName ,String Url,String strSiteID,String strColumnID,String strDespatcher,String strMechanism,String strOther1){ 
  27.  try { 
  28.  String fname =fileName;//要上傳的文件 
  29.  File file = new File(fname); 
  30.  URL url = new URL(Url+"/cms/infoShare/httpURLConnection.jsp"); 
  31.  HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 
  32.  conn.setDoOutput(true); 
  33.  conn.setDoInput(true); 
  34.  conn.setChunkedStreamingMode(1024 * 1024); 
  35.  conn.setRequestMethod("POST"); 
  36.  conn.setRequestProperty("connection""Keep-Alive"); 
  37.  conn.setRequestProperty("Charsert""UTF-8"); 
  38.  conn.setConnectTimeout(50000); 
  39.  conn.setRequestProperty("Content-Type""multipart/form-data;file="+ file.getName()); 
  40.  conn.setRequestProperty("fileName",file.getName()); 
  41.  conn.setRequestProperty("strSiteID", strSiteID); 
  42.  conn.setRequestProperty("strColumnID", strColumnID); 
  43.  conn.setRequestProperty("strDespatcher", strDespatcher); 
  44.  conn.setRequestProperty("strMechanism", strMechanism); 
  45.  conn.setRequestProperty("strOther1", strOther1); 
  46.  conn.connect(); 
  47.  OutputStream out = new DataOutputStream(conn.getOutputStream()); 
  48.  DataInputStream in = new DataInputStream(new FileInputStream(file)); 
  49.  int bytes = 0; 
  50.  byte[] bufferOut = new byte[2048]; 
  51.  while ((bytes = in.read(bufferOut)) != -1) { 
  52.  out.write(bufferOut, 0, bytes); 
  53.  } 
  54.  in.close(); 
  55.  
  56.  out.flush(); 
  57.  out.close(); 
  58.  BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream())); 
  59.  String line = null
  60.  while ((line = reader.readLine()) != null) { 
  61.  //System.out.println("---line---"+line); 
  62.  } 
  63.  } catch (Exception e) { 
  64.  e.printStackTrace(); 
  65.  }finally{ 
  66.  new File(fileName).delete(); 
  67.  } 
  68.  } 
  69.  

【本文是51CTO專欄作者張勇波的原創(chuàng)文章,轉(zhuǎn)載請(qǐng)通過51CTO獲取作者授權(quán)】

責(zé)任編輯:武曉燕 來源: 上下求索的Z先生博客
相關(guān)推薦

2014-08-13 10:20:59

HttpURLConn

2014-08-15 13:11:03

HttpURLConn

2010-01-25 11:09:58

Android Htt

2016-03-24 13:57:59

JavaHttpURLConn

2009-08-13 15:18:23

C#文件上傳

2009-07-14 17:20:31

Webwork文件上傳

2011-12-30 09:49:36

Silverlight

2021-01-15 11:40:44

文件Java秒傳

2017-12-01 10:13:42

前端操作上傳

2009-11-16 09:35:42

PHP上傳

2011-02-23 17:24:31

FileZilla

2009-11-16 10:49:43

PHP上傳文件代碼

2009-11-16 11:41:19

PHP上傳大文件

2022-06-13 14:06:33

大文件上傳前端

2009-10-23 10:45:33

linux系統(tǒng)文件下載

2011-09-14 09:20:03

PhonegapAndroid平臺(tái)

2009-11-16 10:40:02

PHP上傳文件代碼

2010-04-07 11:09:53

2009-11-16 10:16:24

PHP文件上傳

2024-11-12 09:54:23

點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)