一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|編程技術(shù)|正則表達(dá)式|

服務(wù)器之家 - 編程語(yǔ)言 - JAVA教程 - 利用HttpUrlConnection 上傳 接收文件的實(shí)現(xiàn)方法

利用HttpUrlConnection 上傳 接收文件的實(shí)現(xiàn)方法

2020-07-06 14:29jingxian JAVA教程

下面小編就為大家?guī)硪黄肏ttpUrlConnection 上傳 接收文件的實(shí)現(xiàn)方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

如下所示:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
//客戶端代碼
 
public static void main(String[] args) throws IOException {
 DataInputStream in = null;
 OutputStream out = null;
 HttpURLConnection conn = null;
 JSONObject resposeTxt = null;
 InputStream ins = null;
 ByteArrayOutputStream outStream = null;
 try {
  URL url = new URL("http://10.28.160.160:9080/main/uploadFile?fileName=列表.txt");
  conn = (HttpURLConnection) url.openConnection();
  // 發(fā)送POST請(qǐng)求必須設(shè)置如下兩行
  conn.setDoOutput(true);
  conn.setUseCaches(false);
  conn.setRequestMethod("POST");
  conn.setRequestProperty("Content-Type", "text/html");
  conn.setRequestProperty("Cache-Control", "no-cache");
  conn.setRequestProperty("Charsert", "UTF-8");
  conn.connect();
  conn.setConnectTimeout(10000);
  out = conn.getOutputStream();
 
  File file = new File("H:/Users/chengtingyu/Desktop/test/list.txt");
  in = new DataInputStream(new FileInputStream(file));
 
  int bytes = 0;
  byte[] buffer = new byte[1024];
  while ((bytes = in.read(buffer)) != -1) {
  out.write(buffer, 0, bytes);
  }
  out.flush();
 
  // 返回流
  if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
  ins = conn.getInputStream();
  outStream = new ByteArrayOutputStream();
  byte[] data = new byte[1024];
  int count = -1;
  while ((count = ins.read(data, 0, 1024)) != -1) {
   outStream.write(data, 0, count);
  }
  data = null;
  resposeTxt = JSONObject.parseObject(new String(outStream
   .toByteArray(), "UTF-8"));
  }
 } catch (Exception e) {
  e.printStackTrace();
 } finally {
  if (in != null) {
  in.close();
  }
  if (out != null) {
  out.close();
  }
  if (ins != null) {
  ins.close();
  }
  if (outStream != null) {
  outStream.close();
  }
  if (conn != null) {
  conn.disconnect();
  }
 }
 }
 
 
 
//服務(wù)端代碼
 
 
 
public String uploadFile() throws Exception{
    String fileName = request.getParameter("fileName"); 
    String fileFullPath = "H:/Users/chengtingyu/Desktop/" + fileName;
    InputStream input = null;
    FileOutputStream fos = null;
 try {
  input = request.getInputStream();
  File file = new File("H:/Users/chengtingyu/Desktop"); 
     if(!file.exists()){ 
       file.mkdirs(); 
     
     fos = new FileOutputStream(fileFullPath); 
     int size = 0
     byte[] buffer = new byte[1024]; 
     while ((size = input.read(buffer,0,1024)) != -1) { 
       fos.write(buffer, 0, size); 
     }
     
     //響應(yīng)信息 json字符串格式
     Map<String,Object> responseMap = new HashMap<String,Object>();
     responseMap.put("flag", true);
     
     //生成響應(yīng)的json字符串
      String jsonResponse = JSONObject.toJSONString(responseMap);
     sendResponse(jsonResponse);
 } catch (IOException e) {
  //響應(yīng)信息 json字符串格式
     Map<String,Object> responseMap = new HashMap<String,Object>();
     responseMap.put("flag", false);
     responseMap.put("errorMsg", e.getMessage());
     String jsonResponse = JSONObject.toJSONString(responseMap);
     sendResponse(jsonResponse);
 } finally{
  if(input != null){
  input.close();
  }
  if(fos != null){
  fos.close();
  }
 
      
    return null;
 }
 
 
 
 /**
   * 返回響應(yīng)
   *
   * @throws Exception
   */
  private void sendResponse(String responseString) throws Exception {
   response.setContentType("application/json;charset=UTF-8");
    PrintWriter pw = null;
    try {
      pw = response.getWriter();
      pw.write(responseString);
      pw.flush();
    } finally {
      IOUtils.closeQuietly(pw);
    }
  }

以上這篇利用HttpUrlConnection 上傳 接收文件的實(shí)現(xiàn)方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持服務(wù)器之家。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 日韩网站在线 | 亚洲国产在线午夜视频无 | 欧美日韩国产最新一区二区 | 国产51| sxx免费看视频在线播放 | 草莓视频在线观看免费 | 欧美x×x| 全黄毛片 | 美女的让男人桶爽免费看 | 疯狂刺激的3p国产在线 | 女同videos双性人 | 成年性午夜免费视频网站不卡 | 大叔在线观看 | 欧美性色欧美a在线播放 | 236zz宅宅最新伦理 | 国产一区二区三区四卡 | 国产精品馆 | 欧美亚洲一区二区三区 | 久久亚洲精品AV无码四区 | 成人免费淫片95视频观看网站 | 无码射肉在线播放视频 | 精品国产品国语在线不卡丶 | 国产好深好硬好爽我还要视频 | 99免费精品| a黄毛片| 欧美同志网址 | 国产一区二区三区在线观看视频 | 天天插综合网 | 风间由美一区二区播放合集 | 日本高清在线看免费观看 | 歪歪动漫小说sss | 99久久精品国语对白 | 国产精品美女福利视频免费专区 | 91在线 在线播放 | 天天狠天天天天透在线 | 成人另类视频 | 春意影院午夜爽爽爽免费 | 小泽玛丽av无码观看 | 操穴勤 | 成人四虎 | 狠狠综合网|