亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

為什么我自己寫的upload方法不能用

//獲取mediaid,上傳文件到微信客戶端
public static String getMediaId(String filePath,String accessToken,String type) throws Exception {
? ?File file = new File(filePath);
? ?if(!file.exists()){
? ? ? ?throw new IOException("文件不存在哦");
? ?}

? ?String url = UPLOAD_URL.replace("ACCESS_TOKEN",accessToken).replace("TYPE",type);

? ?URL urlObject = new URL(url);

? ?HttpURLConnection con = (HttpURLConnection) urlObject.openConnection();
? ?//設置連接信息
? ?con.setRequestMethod("POST");
? ?con.setDoInput(true);
? ?con.setDoOutput(true);
? ?con.setUseCaches(false);
? ?//設置請求頭信息
? ?con.setRequestProperty("Connection","Keep-Alive");
? ?con.setRequestProperty("Charset","UTF-8");
? ?//設置邊界
? ?String BOUNDARY = "----------"+System.currentTimeMillis();
? ?con.setRequestProperty("Content-Type","mutipart/form-data;boundary="+BOUNDARY);

? ?//設置請求體的頭部
? ?/*
? ? ? ?-----------------------------7e15d2960544
? ? ? ?Content-Disposition: form-data; name="file"; filename="C:\Users\admin\Desktop\hello.txt"
? ? ? ?Content-Type: text/plain

? ? ? ?hello
? ? ? ?-----------------------------7e15d2960544--
? ? */
? ?//將請求體的頭部信息寫入con獲取的outStream中
? ?StringBuffer sb ?=new StringBuffer();
? ?sb.append("--");
? ?sb.append(BOUNDARY+"\r\n");
? ?sb.append("Content-Disposition: form-data;name=\"file\";filename=\"" + file.getName() + "\"\r\n");
? ?sb.append("Content-Type:application/octet-stream\r\n\r\n");

? ?System.out.println("文件的請求頭部信息是:");
? ?System.out.println(sb.toString());

? ?byte[] requestHead = sb.toString().getBytes("UTF-8");
? ?//獲取輸入流
? ?OutputStream out = new DataOutputStream(con.getOutputStream());

? ?//寫入請求頭的一些基本信息
? ?out.write(requestHead);
? ?//處理文件正文部分
? ?DataInputStream in = new DataInputStream(new FileInputStream(file));
? ?byte[] b = new byte[1024];
? ?int n;
? ?while((n=in.read(b))!=-1){
? ? ? ?System.out.println(n);
? ? ? ?out.write(b,0,n);
? ?}
? ?in.close();

? ?//處理結尾部分
? ?byte[] foot = ("\r\n--" + BOUNDARY + "--\r\n").getBytes("utf-8");
? ?System.out.println("\r\n--" + BOUNDARY + "--\r\n");
? ?out.write(foot);
? ?out.flush();

? ?out.close();

? ?//開始讀取微信客戶端返回的數據
? ?StringBuffer buffer = ?new StringBuffer();;
? ?BufferedReader br = null;
? ?String result = null;
? ?try {
? ? ? ? br = new BufferedReader(new InputStreamReader(con.getInputStream()));
? ? ? ? String line ;
? ? ? ? while((line = br.readLine())!=null){
? ? ? ? ? ?buffer.append(line);
? ? ? ?}
? ? ? ?if(result ==null){
? ? ? ? ? ?result = buffer.toString();
? ? ? ?}
? ?} catch (IOException e) {
? ? ? ?e.printStackTrace();
? ?} finally {
? ? ? ?br.close();
? ?}
? ?//解析返回的String數據
? ?JSONObject jsonObject = JSONObject.fromObject(result);
? ?System.out.println(jsonObject);
? ?String mediaId = jsonObject.getString("media_id");
? ?return mediaId;
}


結果:

Exception in thread "main" net.sf.json.JSONException: JSONObject["media_id"] not found.

{"errcode":41005,"errmsg":"media data missing hint: [bS31uA0672e565]"}

at net.sf.json.JSONObject.getString(JSONObject.java:2245)

at utils.InterfaceUtils.getMediaId(InterfaceUtils.java:139)

at utils.InterfaceUtils.main(InterfaceUtils.java:144)

這個文件我用OutputStream os = new FileOutputStream()是可以獲取到的,可是就是不行。。。

正在回答

舉報

0/150
提交
取消

為什么我自己寫的upload方法不能用

我要回答 關注問題
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號