當前位置:首頁 » 文件管理 » 微信上傳文件介面

微信上傳文件介面

發布時間: 2023-05-24 01:28:24

『壹』 微信開發平台中有個介面是上傳多媒體文件,我用的是java 開發的,我怎麼樣才能在後台實現呢代碼如下:

/**
*文件上傳到微信伺服器
*@paramfileType文件類型
*@paramfilePath文件路徑
*@returnJSONObject
*@throwsException
*/
publicstaticJSONObjectsend(StringfileType,StringfilePath)throwsException{
Stringresult=null;
Filefile=newFile(filePath);
if(!file.exists()||!file.isFile()){
thrownewIOException("文件不存在");
}
/**
*第一部分
*/
URLurlObj=newURL("http://file.api.weixin.qq.com/cgi-bin/media/upload?access_token="+getAccess_token()+"&type="+fileType+"");
HttpURLConnectioncon=(HttpURLConnection)urlObj.openConnection();
con.setRequestMethod("POST");//以Post方式提交表單,默認get方式
con.setDoInput(true);
con.setDoOutput(true);
con.setUseCaches(false);//post方式不能使用緩存
//設置請求頭信息
con.setRequestProperty("Connection","Keep-Alive");
con.setRequestProperty("Charset","UTF-8");
//設置邊界
StringBOUNDARY="----------"+System.currentTimeMillis();
con.setRequestProperty("Content-Type","multipart/form-data;boundary="+BOUNDARY);
//請求正文信息
//第一部分:
StringBuildersb=newStringBuilder();
sb.append("--");//必須多兩道線
sb.append(BOUNDARY);
sb.append(" ");
sb.append("Content-Disposition:form-data;name="file";filename=""+file.getName()+"" ");
sb.append("Content-Type:application/octet-stream ");
byte[]head=sb.toString().getBytes("utf-8");
//獲得輸出流
OutputStreamout=newDataOutputStream(con.getOutputStream());
//輸出表頭
out.write(head);
//文件正文部分
//把文件已流文件的方式推入到url中
DataInputStreamin=newDataInputStream(newFileInputStream(file));
intbytes=0;
byte[]bufferOut=newbyte[1024];
while((bytes=in.read(bufferOut))!=-1){
out.write(bufferOut,0,bytes);
}
in.close();
//結尾部分
byte[]foot=(" --"+BOUNDARY+"-- ").getBytes("utf-8");//定義最後數據分隔線
out.write(foot);
out.flush();
out.close();
StringBufferbuffer=newStringBuffer();
BufferedReaderreader=null;
try{
//定義BufferedReader輸入流來讀取URL的響應
reader=newBufferedReader(newInputStreamReader(con.getInputStream()));
Stringline=null;
while((line=reader.readLine())!=null){
//System.out.println(line);
buffer.append(line);
}
if(result==null){
result=buffer.toString();
}
}catch(IOExceptione){
System.out.println("發送POST請求出現異常!"+e);
e.printStackTrace();
thrownewIOException("數據讀取異常");
}finally{
if(reader!=null){
reader.close();
}
}
JSONObjectjsonObj=newJSONObject(result);
returnjsonObj;
}

『貳』 如何向微信伺服器上傳文件

在某些開發中,我們需要向微信伺服器發送文件,比如圖片,語音等等,藉助微信伺服器來實現我們的一些需求,具體的實現如下:

/**
* 向微信伺服器上傳文件
*
* @param accessToken
* 進入的介面
* @param type
* 文件類型(語音或者是圖片)(對於文檔不適合)
* @param url
* 文件的存儲路徑
* @return json的格式{"media_id":
* "nrSKG2eY1E__pdiNiSXuijbCmWWc8unzBQ"
* ,"created_at":1408436207,"type":"image"}
*/
public JSONObject uploadFile(String fileType, String filePath)
throws Exception {
GetExistAccessToken getExistAccessToken = GetExistAccessToken.getInstance();
String accessToken = getExistAccessToken.getExistAccessToken();
// 上傳文件請求路徑
String action = "http://file.api.weixin.qq.com/cgi-bin/media/upload?access_token="
+ accessToken + "&type=" + fileType;
URL url = new URL(action);
String result = null;
File file = new File(filePath);
if (!file.exists() || !file.isFile()) {
throw new IOException("上傳的文件不存在");
}
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("POST"); // 以Post方式提交表單,默認get方式
con.setDoInput(true);
con.setDoOutput(true);
con.setUseCaches(false); // post方式不能使用緩存
// 設置請求頭信息
con.setRequestProperty("Connection", "Keep-Alive");
con.setRequestProperty("Charset", "UTF-8");
// 設置邊界
String BOUNDARY = "----------" + System.currentTimeMillis();
con.setRequestProperty("Content-Type", "multipart/form-data; boundary="
+ BOUNDARY);
// 請求正文信息
// 第一部分:
StringBuilder sb = new StringBuilder();
sb.append("--"); // 必須多兩道線
sb.append(BOUNDARY);
sb.append("\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");
byte[] head = sb.toString().getBytes("utf-8");
// 獲得輸出流
OutputStream out = new DataOutputStream(con.getOutputStream());
// 輸出表頭
out.write(head);
// 文件正文部分
// 把文件已流文件的方式 推入到url中
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);
}
in.close();
// 結尾部分
byte[] foot = ("\r\n--" + BOUNDARY + "--\r\n").getBytes("utf-8");// 定義最後數據分隔線
out.write(foot);
out.flush();
out.close();
StringBuffer buffer = new StringBuffer();
BufferedReader reader = null;
try {
// 定義BufferedReader輸入流來讀取URL的響應
reader = new BufferedReader(new InputStreamReader(con
.getInputStream()));
String line = null;
while ((line = reader.readLine()) != null) {
buffer.append(line);
}
if (result == null) {
result = buffer.toString();
}
} catch (IOException e) {
System.out.println("發送POST請求出現異常!" + e);
e.printStackTrace();
throw new IOException("數據讀取異常");
} finally {
if (reader != null) {
reader.close();
}
}
JSONObject jsonObj = new JSONObject(result);
return jsonObj;
}

『叄』 微信小程序-上傳文件 (chooseMessageFile)

微信液鉛小程序有 wx.chooseImage 方法選擇圖片,但是想選擇文件,chooseFile這種接早嫌口卻沒有提供。

還好提供陸埋手了其它方法

到微信的消息欄選擇文件助手後

參考
https://developers.weixin.qq.com/community/develop/doc/
https://developers.weixin.qq.com/miniprogram/dev/api/media/image/wx.chooseMessageFile.html

熱點內容
tomcat上傳超時 發布:2025-02-09 01:41:42 瀏覽:483
androidactivity豎屏 發布:2025-02-09 01:41:40 瀏覽:377
家庭配置怎麼合理 發布:2025-02-09 01:36:14 瀏覽:807
頭條軍事源碼 發布:2025-02-09 01:31:53 瀏覽:997
androidintent視頻 發布:2025-02-09 01:31:43 瀏覽:858
歐姆龍plc密碼如何設置 發布:2025-02-09 01:24:31 瀏覽:687
php支持jpeg 發布:2025-02-09 01:24:22 瀏覽:803
反編譯去注冊碼 發布:2025-02-09 01:19:48 瀏覽:887
安卓如何查找舊密碼 發布:2025-02-09 01:17:21 瀏覽:418
hadoop雲存儲 發布:2025-02-09 01:02:49 瀏覽:453