me.chanjar.weixin.common.bean.result.WxMediaUploadResult Java Examples
The following examples show how to use
me.chanjar.weixin.common.bean.result.WxMediaUploadResult.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MediaUploadRequestExecutor.java From weixin-java-tools with Apache License 2.0 | 6 votes |
@Override public WxMediaUploadResult execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, File file) throws WxErrorException, ClientProtocolException, IOException { HttpPost httpPost = new HttpPost(uri); if (httpProxy != null) { RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build(); httpPost.setConfig(config); } if (file != null) { HttpEntity entity = MultipartEntityBuilder .create() .addBinaryBody("media", file) .setMode(HttpMultipartMode.RFC6532) .build(); httpPost.setEntity(entity); httpPost.setHeader("Content-Type", ContentType.MULTIPART_FORM_DATA.toString()); } try (CloseableHttpResponse response = httpclient.execute(httpPost)) { String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response); WxError error = WxError.fromJson(responseContent); if (error.getErrorCode() != 0) { throw new WxErrorException(error); } return WxMediaUploadResult.fromJson(responseContent); } }
Example #2
Source File: WxMediaUploadResultAdapter.java From weixin-java-tools with Apache License 2.0 | 6 votes |
public WxMediaUploadResult deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { WxMediaUploadResult uploadResult = new WxMediaUploadResult(); JsonObject uploadResultJsonObject = json.getAsJsonObject(); if (uploadResultJsonObject.get("type") != null && !uploadResultJsonObject.get("type").isJsonNull()) { uploadResult.setType(GsonHelper.getAsString(uploadResultJsonObject.get("type"))); } if (uploadResultJsonObject.get("media_id") != null && !uploadResultJsonObject.get("media_id").isJsonNull()) { uploadResult.setMediaId(GsonHelper.getAsString(uploadResultJsonObject.get("media_id"))); } if (uploadResultJsonObject.get("thumb_media_id") != null && !uploadResultJsonObject.get("thumb_media_id").isJsonNull()) { uploadResult.setThumbMediaId(GsonHelper.getAsString(uploadResultJsonObject.get("thumb_media_id"))); } if (uploadResultJsonObject.get("created_at") != null && !uploadResultJsonObject.get("created_at").isJsonNull()) { uploadResult.setCreatedAt(GsonHelper.getAsPrimitiveLong(uploadResultJsonObject.get("created_at"))); } return uploadResult; }
Example #3
Source File: WxCpMediaServiceImplTest.java From weixin-java-tools with Apache License 2.0 | 6 votes |
@Test(dataProvider = "mediaData") public void testUploadMedia(String mediaType, String fileType, String fileName) throws WxErrorException, IOException { try (InputStream inputStream = ClassLoader.getSystemResourceAsStream(fileName)) { WxMediaUploadResult res = this.wxService.getMediaService().upload(mediaType, fileType, inputStream); assertNotNull(res.getType()); assertNotNull(res.getCreatedAt()); assertTrue(res.getMediaId() != null || res.getThumbMediaId() != null); if (res.getMediaId() != null) { this.mediaIds.add(res.getMediaId()); } if (res.getThumbMediaId() != null) { this.mediaIds.add(res.getThumbMediaId()); } } }
Example #4
Source File: JoddHttpMediaUploadRequestExecutor.java From weixin-java-tools with Apache License 2.0 | 6 votes |
@Override public WxMediaUploadResult execute(String uri, File file) throws WxErrorException, IOException { HttpRequest request = HttpRequest.post(uri); if (requestHttp.getRequestHttpProxy() != null) { requestHttp.getRequestHttpClient().useProxy(requestHttp.getRequestHttpProxy()); } request.withConnectionProvider(requestHttp.getRequestHttpClient()); request.form("media", file); HttpResponse response = request.send(); response.charset(StringPool.UTF_8); String responseContent = response.bodyText(); WxError error = WxError.fromJson(responseContent); if (error.getErrorCode() != 0) { throw new WxErrorException(error); } return WxMediaUploadResult.fromJson(responseContent); }
Example #5
Source File: ApacheMediaUploadRequestExecutor.java From weixin-java-tools with Apache License 2.0 | 6 votes |
@Override public WxMediaUploadResult execute(String uri, File file) throws WxErrorException, IOException { HttpPost httpPost = new HttpPost(uri); if (requestHttp.getRequestHttpProxy() != null) { RequestConfig config = RequestConfig.custom().setProxy(requestHttp.getRequestHttpProxy()).build(); httpPost.setConfig(config); } if (file != null) { HttpEntity entity = MultipartEntityBuilder .create() .addBinaryBody("media", file) .setMode(HttpMultipartMode.RFC6532) .build(); httpPost.setEntity(entity); } try (CloseableHttpResponse response = requestHttp.getRequestHttpClient().execute(httpPost)) { String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response); WxError error = WxError.fromJson(responseContent); if (error.getErrorCode() != 0) { throw new WxErrorException(error); } return WxMediaUploadResult.fromJson(responseContent); } finally { httpPost.releaseConnection(); } }
Example #6
Source File: DemoImageHandler.java From weixin-java-tools with Apache License 2.0 | 6 votes |
@Override public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage, Map<String, Object> context, WxMpService wxMpService, WxSessionManager sessionManager) { try { WxMediaUploadResult wxMediaUploadResult = wxMpService.getMaterialService() .mediaUpload(WxConsts.MediaFileType.IMAGE, TestConstants.FILE_JPG, ClassLoader.getSystemResourceAsStream("mm.jpeg")); WxMpXmlOutImageMessage m = WxMpXmlOutMessage .IMAGE() .mediaId(wxMediaUploadResult.getMediaId()) .fromUser(wxMessage.getToUser()) .toUser(wxMessage.getFromUser()) .build(); return m; } catch (WxErrorException e) { e.printStackTrace(); } return null; }
Example #7
Source File: WxMpMaterialServiceImplTest.java From weixin-java-tools with Apache License 2.0 | 6 votes |
@Test(dataProvider = "mediaFiles") public void testUploadMedia(String mediaType, String fileType, String fileName) throws WxErrorException, IOException { try (InputStream inputStream = ClassLoader.getSystemResourceAsStream(fileName)) { WxMediaUploadResult res = this.wxService.getMaterialService().mediaUpload(mediaType, fileType, inputStream); assertNotNull(res.getType()); assertNotNull(res.getCreatedAt()); assertTrue(res.getMediaId() != null || res.getThumbMediaId() != null); if (res.getMediaId() != null && !mediaType.equals(WxConsts.MediaFileType.VIDEO)) { //video 不支持下载,所以不加入 this.mediaIdsToDownload.add(res.getMediaId()); } if (res.getThumbMediaId() != null) { this.mediaIdsToDownload.add(res.getThumbMediaId()); } System.out.println(res); } }
Example #8
Source File: OkHttpMediaUploadRequestExecutor.java From weixin-java-tools with Apache License 2.0 | 6 votes |
@Override public WxMediaUploadResult execute(String uri, File file) throws WxErrorException, IOException { logger.debug("OkHttpMediaUploadRequestExecutor is running"); //得到httpClient OkHttpClient client = requestHttp.getRequestHttpClient(); RequestBody body = new MultipartBody.Builder() .setType(MediaType.parse("multipart/form-data")) .addFormDataPart("media", file.getName(), RequestBody.create(MediaType.parse("application/octet-stream"), file)) .build(); Request request = new Request.Builder().url(uri).post(body).build(); Response response = client.newCall(request).execute(); String responseContent = response.body().string(); WxError error = WxError.fromJson(responseContent); if (error.getErrorCode() != 0) { throw new WxErrorException(error); } return WxMediaUploadResult.fromJson(responseContent); }
Example #9
Source File: WxMediaUploadResultAdapter.java From weixin-java-tools with Apache License 2.0 | 6 votes |
@Override public WxMediaUploadResult deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { WxMediaUploadResult uploadResult = new WxMediaUploadResult(); JsonObject uploadResultJsonObject = json.getAsJsonObject(); if (uploadResultJsonObject.get("type") != null && !uploadResultJsonObject.get("type").isJsonNull()) { uploadResult.setType(GsonHelper.getAsString(uploadResultJsonObject.get("type"))); } if (uploadResultJsonObject.get("media_id") != null && !uploadResultJsonObject.get("media_id").isJsonNull()) { uploadResult.setMediaId(GsonHelper.getAsString(uploadResultJsonObject.get("media_id"))); } if (uploadResultJsonObject.get("thumb_media_id") != null && !uploadResultJsonObject.get("thumb_media_id").isJsonNull()) { uploadResult.setThumbMediaId(GsonHelper.getAsString(uploadResultJsonObject.get("thumb_media_id"))); } if (uploadResultJsonObject.get("created_at") != null && !uploadResultJsonObject.get("created_at").isJsonNull()) { uploadResult.setCreatedAt(GsonHelper.getAsPrimitiveLong(uploadResultJsonObject.get("created_at"))); } return uploadResult; }
Example #10
Source File: WxMaDemoServer.java From weixin-java-tools with Apache License 2.0 | 6 votes |
@Override public void handle(WxMaMessage wxMessage, Map<String, Object> context, WxMaService service, WxSessionManager sessionManager) throws WxErrorException { try { final File file = service.getQrcodeService().createQrcode("123", 430); WxMediaUploadResult uploadResult = service.getMediaService().uploadMedia(WxMaConstants.MediaType.IMAGE, file); service.getMsgService().sendKefuMsg( WxMaKefuMessage .newImageBuilder() .mediaId(uploadResult.getMediaId()) .toUser(wxMessage.getFromUser()) .build()); } catch (WxErrorException e) { e.printStackTrace(); } }
Example #11
Source File: WxMaDemoServer.java From weixin-java-tools with Apache License 2.0 | 6 votes |
@Override public void handle(WxMaMessage wxMessage, Map<String, Object> context, WxMaService service, WxSessionManager sessionManager) throws WxErrorException { try { WxMediaUploadResult uploadResult = service.getMediaService() .uploadMedia(WxMaConstants.MediaType.IMAGE, "png", ClassLoader.getSystemResourceAsStream("tmp.png")); service.getMsgService().sendKefuMsg( WxMaKefuMessage .newImageBuilder() .mediaId(uploadResult.getMediaId()) .toUser(wxMessage.getFromUser()) .build()); } catch (WxErrorException e) { e.printStackTrace(); } }
Example #12
Source File: WxMaMediaServiceImplTest.java From weixin-java-tools with Apache License 2.0 | 5 votes |
@Test public void testUploadMedia() throws WxErrorException, IOException { String mediaType = "image"; String fileType = "png"; String fileName = "tmp.png"; try (InputStream inputStream = ClassLoader.getSystemResourceAsStream(fileName)) { WxMediaUploadResult res = this.wxService.getMediaService().uploadMedia(mediaType, fileType, inputStream); assertNotNull(res.getType()); assertNotNull(res.getCreatedAt()); assertTrue(res.getMediaId() != null || res.getThumbMediaId() != null); this.mediaId = res.getMediaId(); System.out.println(res); } }
Example #13
Source File: WxMpMediaAPITest.java From weixin-java-tools with Apache License 2.0 | 5 votes |
@Test(dataProvider="uploadMedia") public void testUploadMedia(String mediaType, String fileType, String fileName) throws WxErrorException, IOException { InputStream inputStream = ClassLoader.getSystemResourceAsStream(fileName); WxMediaUploadResult res = wxService.mediaUpload(mediaType, fileType, inputStream); Assert.assertNotNull(res.getType()); Assert.assertNotNull(res.getCreatedAt()); Assert.assertTrue(res.getMediaId() != null || res.getThumbMediaId() != null); if (res.getMediaId() != null) { media_ids.add(res.getMediaId()); } if (res.getThumbMediaId() != null) { media_ids.add(res.getThumbMediaId()); } }
Example #14
Source File: WxCpMediaAPITest.java From weixin-java-tools with Apache License 2.0 | 5 votes |
@Test(dataProvider="uploadMedia") public void testUploadMedia(String mediaType, String fileType, String fileName) throws WxErrorException, IOException { InputStream inputStream = ClassLoader.getSystemResourceAsStream(fileName); WxMediaUploadResult res = wxService.mediaUpload(mediaType, fileType, inputStream); Assert.assertNotNull(res.getType()); Assert.assertNotNull(res.getCreatedAt()); Assert.assertTrue(res.getMediaId() != null || res.getThumbMediaId() != null); if (res.getMediaId() != null) { media_ids.add(res.getMediaId()); } if (res.getThumbMediaId() != null) { media_ids.add(res.getThumbMediaId()); } }
Example #15
Source File: WxMaMediaController.java From sdb-mall with Apache License 2.0 | 5 votes |
/** * 上传临时素材 * * @return 素材的media_id列表,实际上如果有的话,只会有一个 */ @PostMapping("/upload") public List<String> uploadMedia(HttpServletRequest request) throws WxErrorException { CommonsMultipartResolver resolver = new CommonsMultipartResolver(request.getSession().getServletContext()); if (!resolver.isMultipart(request)) { return Lists.newArrayList(); } MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request; Iterator<String> it = multiRequest.getFileNames(); List<String> result = Lists.newArrayList(); while (it.hasNext()) { try { MultipartFile file = multiRequest.getFile(it.next()); File newFile = new File(Files.createTempDir(), file.getOriginalFilename()); this.logger.info("filePath is :" + newFile.toString()); file.transferTo(newFile); WxMediaUploadResult uploadResult = this.service.getMediaService().uploadMedia(WxMaConstants.KefuMsgType.IMAGE, newFile); this.logger.info("media_id : " + uploadResult.getMediaId()); result.add(uploadResult.getMediaId()); } catch (IOException e) { this.logger.error(e.getMessage(), e); } } return result; }
Example #16
Source File: MediaUploadRequestExecutor.java From weixin-java-tools with Apache License 2.0 | 5 votes |
public static RequestExecutor<WxMediaUploadResult, File> create(RequestHttp requestHttp) { switch (requestHttp.getRequestType()) { case APACHE_HTTP: return new ApacheMediaUploadRequestExecutor(requestHttp); case JODD_HTTP: return new JoddHttpMediaUploadRequestExecutor(requestHttp); case OK_HTTP: return new OkHttpMediaUploadRequestExecutor(requestHttp); default: return null; } }
Example #17
Source File: WxMaMediaServiceImpl.java From weixin-java-tools with Apache License 2.0 | 5 votes |
@Override public WxMediaUploadResult uploadMedia(String mediaType, String fileType, InputStream inputStream) throws WxErrorException { try { return this.uploadMedia(mediaType, FileUtils.createTmpFile(inputStream, UUID.randomUUID().toString(), fileType)); } catch (IOException e) { throw new WxErrorException(WxError.builder().errorMsg(e.getMessage()).build(), e); } }
Example #18
Source File: WxMpMaterialServiceImpl.java From weixin-java-tools with Apache License 2.0 | 5 votes |
@Override public WxMediaUploadResult mediaUpload(String mediaType, String fileType, InputStream inputStream) throws WxErrorException { File tmpFile = null; try { tmpFile = FileUtils.createTmpFile(inputStream, UUID.randomUUID().toString(), fileType); return this.mediaUpload(mediaType, tmpFile); } catch (IOException e) { throw new WxErrorException(WxError.builder().errorCode(-1).errorMsg(e.getMessage()).build(), e); } finally { if (tmpFile != null) { tmpFile.delete(); } } }
Example #19
Source File: WxMaMediaServiceImpl.java From weixin-java-tools with Apache License 2.0 | 4 votes |
@Override public WxMediaUploadResult uploadMedia(String mediaType, File file) throws WxErrorException { String url = String.format(MEDIA_UPLOAD_URL, mediaType); return this.wxMaService.execute(MediaUploadRequestExecutor.create(this.wxMaService.getRequestHttp()), url, file); }
Example #20
Source File: WxCpMediaServiceImpl.java From weixin-java-tools with Apache License 2.0 | 4 votes |
@Override public WxMediaUploadResult upload(String mediaType, String fileType, InputStream inputStream) throws WxErrorException, IOException { return this.upload(mediaType, FileUtils.createTmpFile(inputStream, UUID.randomUUID().toString(), fileType)); }
Example #21
Source File: WxCpMediaServiceImpl.java From weixin-java-tools with Apache License 2.0 | 4 votes |
@Override public WxMediaUploadResult upload(String mediaType, File file) throws WxErrorException { String url = "https://qyapi.weixin.qq.com/cgi-bin/media/upload?type=" + mediaType; return this.mainService.execute(MediaUploadRequestExecutor.create(this.mainService.getRequestHttp()), url, file); }
Example #22
Source File: WxMpMaterialServiceImpl.java From weixin-java-tools with Apache License 2.0 | 4 votes |
@Override public WxMediaUploadResult mediaUpload(String mediaType, File file) throws WxErrorException { String url = String.format(MEDIA_UPLOAD_URL, mediaType); return this.wxMpService.execute(MediaUploadRequestExecutor.create(this.wxMpService.getRequestHttp()), url, file); }
Example #23
Source File: WxMpKefuServiceImpl.java From weixin-java-tools with Apache License 2.0 | 4 votes |
@Override public boolean kfAccountUploadHeadImg(String kfAccount, File imgFile) throws WxErrorException { WxMediaUploadResult responseContent = this.wxMpService .execute(MediaUploadRequestExecutor.create(this.wxMpService.getRequestHttp()), String.format(KFACCOUNT_UPLOAD_HEAD_IMG, kfAccount), imgFile); return responseContent != null; }
Example #24
Source File: WxMpServiceImpl.java From weixin-java-tools with Apache License 2.0 | 4 votes |
public WxMediaUploadResult mediaUpload(String mediaType, File file) throws WxErrorException { String url = "http://file.api.weixin.qq.com/cgi-bin/media/upload?type=" + mediaType; return execute(new MediaUploadRequestExecutor(), url, file); }
Example #25
Source File: WxCpServiceImpl.java From weixin-java-tools with Apache License 2.0 | 4 votes |
public WxMediaUploadResult mediaUpload(String mediaType, String fileType, InputStream inputStream) throws WxErrorException, IOException { return mediaUpload(mediaType, FileUtils.createTmpFile(inputStream, UUID.randomUUID().toString(), fileType)); }
Example #26
Source File: WxCpServiceImpl.java From weixin-java-tools with Apache License 2.0 | 4 votes |
public WxMediaUploadResult mediaUpload(String mediaType, File file) throws WxErrorException { String url = "https://qyapi.weixin.qq.com/cgi-bin/media/upload?type=" + mediaType; return execute(new MediaUploadRequestExecutor(), url, file); }
Example #27
Source File: WxMpServiceImpl.java From weixin-java-tools with Apache License 2.0 | 4 votes |
public WxMediaUploadResult mediaUpload(String mediaType, String fileType, InputStream inputStream) throws WxErrorException, IOException { return mediaUpload(mediaType, FileUtils.createTmpFile(inputStream, UUID.randomUUID().toString(), fileType)); }
Example #28
Source File: WxCpMediaService.java From weixin-java-tools with Apache License 2.0 | 2 votes |
/** * @param mediaType 媒体类型 * @param file 文件对象 * @see #upload(String, String, InputStream) */ WxMediaUploadResult upload(String mediaType, File file) throws WxErrorException;
Example #29
Source File: WxMpService.java From weixin-java-tools with Apache License 2.0 | 2 votes |
/** * @see #mediaUpload(String, String, InputStream) * @param mediaType * @param file * @throws WxErrorException */ public WxMediaUploadResult mediaUpload(String mediaType, File file) throws WxErrorException;
Example #30
Source File: WxMpService.java From weixin-java-tools with Apache License 2.0 | 2 votes |
/** * <pre> * 上传多媒体文件 * * 上传的多媒体文件有格式和大小限制,如下: * 图片(image): 1M,支持JPG格式 * 语音(voice):2M,播放长度不超过60s,支持AMR\MP3格式 * 视频(video):10MB,支持MP4格式 * 缩略图(thumb):64KB,支持JPG格式 * * 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=上传下载多媒体文件 * </pre> * @param mediaType 媒体类型, 请看{@link me.chanjar.weixin.common.api.WxConsts} * @param fileType 文件类型,请看{@link me.chanjar.weixin.common.api.WxConsts} * @param inputStream 输入流 * @throws WxErrorException */ public WxMediaUploadResult mediaUpload(String mediaType, String fileType, InputStream inputStream) throws WxErrorException, IOException;