企业微信上传文件到服务器,上传素材到腾讯企业微信服务器

//filetype:文件类型;filePath:文件所在路径

@Override

public JSONObject uploadMedia(String fileType, String filePath) throws Exception {

// 返回结果

String result = null;

File file = new File(filePath);

if (!file.exists() || !file.isFile()) {

throw new IOException("文件不存在");

}

String token = publicService.getAppMedssageToken();

if (token == null) {

throw new IOException("未获取到token");

}

String uploadTempMaterial_url = "https://qyapi.weixin.qq.com/cgi-bin/media/upload?access_token=ACCESS_TOKEN&type=TYPE";

uploadTempMaterial_url = uploadTempMaterial_url.replace("ACCESS_TOKEN", token).replace("TYPE", fileType);

URL url = new URL(uploadTempMaterial_url);

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

conn.setRequestMethod("POST");// 以POST方式提交表单

conn.setDoInput(true);

conn.setDoOutput(true);

conn.setUseCaches(false);// POST方式不能使用缓存

// 设置请求头信息

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

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

// 设置边界

String BOUNDARY = "----------" + System.currentTimeMillis();

conn.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=\"media\"; filename=\"" + file.getName() + "\"\r\n");

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

// 获得输出流

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

// 输出表头

out.write(sb.toString().getBytes("UTF-8"));

// 文件正文部分

// 把文件以流的方式 推送道URL中

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

int bytes = 0;

byte[] buffer = new byte[1024];

while ((bytes = din.read(buffer)) != -1) {

out.write(buffer, 0, bytes);

}

din.close();

// 结尾部分

byte[] foot = ("\r\n--" + BOUNDARY + "--\r\n").getBytes("UTF-8");// 定义数据最后分割线

out.write(foot);

out.flush();

out.close();

if (HttpsURLConnection.HTTP_OK == conn.getResponseCode()) {

StringBuffer strbuffer = null;

BufferedReader reader = null;

try {

strbuffer = new StringBuffer();

reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));

String lineString = null;

while ((lineString = reader.readLine()) != null) {

strbuffer.append(lineString);

}

if (result == null) {

result = strbuffer.toString();

}

} catch (IOException e) {

e.printStackTrace();

} finally {

if (reader != null) {

reader.close();

}

}

}

JSONObject jsonObject = JSONObject.parseObject(result);

return jsonObject;

}

标签:sb,String,微信,append,服务器,new,null,上传,conn

来源: https://www.cnblogs.com/Marlo/p/15019929.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值