{"id":28810,"date":"2024-11-25T10:33:42","date_gmt":"2024-11-25T02:33:42","guid":{"rendered":"https:\/\/fwq.ai\/blog\/28810\/"},"modified":"2024-11-25T10:33:42","modified_gmt":"2024-11-25T02:33:42","slug":"%e5%be%ae%e4%bf%a1%e5%bc%80%e5%8f%91%e4%b9%8b%e8%b0%83%e8%b5%b7%e6%91%84%e5%83%8f%e5%a4%b4%e3%80%81%e6%9c%ac%e5%9c%b0%e5%b1%95%e7%a4%ba%e5%9b%be%e7%89%87%e3%80%81%e4%b8%8a%e4%bc%a0%e4%b8%8b%e8%bd%bd","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/28810\/","title":{"rendered":"\u5fae\u4fe1\u5f00\u53d1\u4e4b\u8c03\u8d77\u6444\u50cf\u5934\u3001\u672c\u5730\u5c55\u793a\u56fe\u7247\u3001\u4e0a\u4f20\u4e0b\u8f7d\u56fe\u7247\u65b9\u6cd5"},"content":{"rendered":"<p>\u672c\u6587\u4ecb\u7ecd\u5fae\u4fe1\u5f00\u53d1\u4e4b\u8c03\u8d77\u6444\u50cf\u5934\u3001\u672c\u5730\u5c55\u793a\u56fe\u7247\u3001\u4e0a\u4f20\u4e0b\u8f7d\u56fe\u7247\u65b9\u6cd5<\/p>\n<h1>1.\u914d\u7f6e<\/h1>\n<p>\u9875\u9762\u5f15\u5165\u901a\u8fc7jssdk\u6388\u6743\u540e\uff0c\u4f20\u5165wx\u5bf9\u8c61\uff0c\u9996\u5148\u914d\u7f6e\u9700\u8981\u7684\u63a5\u53e3<\/p>\n<p><\/p>\n<pre>wx.config({    \/* debug: true,  *\/\r\n    appId: appid, \r\n    timestamp: timestamp, \r\n    nonceStr: nonceStr, \r\n    signature: signature,\r\n    jsApiList: [\r\n         'chooseImage',\/\/\u62cd\u7167\u6216\u4ece\u624b\u673a\u76f8\u518c\u4e2d\u9009\u56fe\u63a5\u53e3\r\n         'previewImage',\/\/\u9884\u89c8\u56fe\u7247\u63a5\u53e3\r\n         'uploadImage',\/\/\u4e0a\u4f20\u56fe\u7247\u63a5\u53e3\r\n         'downloadImage'\/\/\u4e0b\u8f7d\u56fe\u7247\u63a5\u53e3\r\n \u3000\u3000]\r\n });<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>&nbsp;<\/p>\n<h1>2.\u8c03\u8d77\u62cd\u7167\/\u76f8\u518c<\/h1>\n<p>\u5c06\u4e0b\u9762\u7684\u65b9\u6cd5\u653e\u5728\u9700\u8981\u70b9\u51fb\u4e8b\u4ef6\u7684\u56de\u8c03\u51fd\u6570\u91cc\u9762&nbsp;<\/p>\n<p><\/p>\n<pre>wx.chooseImage({\r\n    count: 1, \r\n    sizeType: ['compressed'], \r\n    sourceType: ['album', 'camera'], \r\n    success: function (res) {\r\n\u3000\u3000\u3000\u3000\/\/var localIds = res.localIds;\r\n    \u3000 $('.driver-card img').prop('src',res.localIds[0]);\r\n    \u3000 uploadPhoto.uploadToWeixinServer(res.localIds[0],'car')\r\n   }\r\n});<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>&nbsp;<\/p>\n<p>\u8fd9\u65f6\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u8fd9\u6837\u7684\u6548\u679c\uff0c\u4ee3\u8868\u8c03\u8d77\u6210\u529f\u4e86\uff01chooseImage\u65b9\u6cd5\u7684\u6210\u529f\u56de\u8c03\u91cc\uff0c\u6211\u5c06\u9009\u4e2d\u7684\u7167\u7247\u8d4b\u503c\u7ed9\u9700\u8981\u663e\u793a\u7684img\u7684src\uff08\u56e0\u4e3a\u6211\u8fd9\u91cc\u53ea\u6709\u4e00\u5f20\u7167\u7247\uff0c\u5982\u679c\u6709\u591a\u5f20\u7528\u5faa\u73af\u8d4b\u503c\u5373\u53ef\uff09\uff0c\u8fd9\u6837\u4e00\u6765\uff0c\u5c31\u53ef\u4ee5\u76f4\u63a5\u663e\u793a\u521a\u521a\u62cd\u7167\/\u76f8\u518c\u91cc\u9009\u4e2d\u7684\u7167\u7247\u4e86<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/000\/013\/708f8b07310f1dd98be6313faa618d07-0.png\" style=\"max-width:90%\" class=\"aligncenter\" title=\"\u5fae\u4fe1\u5f00\u53d1\u4e4b\u8c03\u8d77\u6444\u50cf\u5934\u3001\u672c\u5730\u5c55\u793a\u56fe\u7247\u3001\u4e0a\u4f20\u4e0b\u8f7d\u56fe\u7247\u65b9\u6cd5\u63d2\u56fe\" alt=\"\u5fae\u4fe1\u5f00\u53d1\u4e4b\u8c03\u8d77\u6444\u50cf\u5934\u3001\u672c\u5730\u5c55\u793a\u56fe\u7247\u3001\u4e0a\u4f20\u4e0b\u8f7d\u56fe\u7247\u65b9\u6cd5\u63d2\u56fe\" \/><\/p>\n<p>&nbsp;<\/p>\n<h1>3.\u4e0a\u4f20\u7167\u7247<\/h1>\n<p>\u5728\u4e0a\u9762chooseImage\u7684success\u56de\u8c03\u91cc\u9762\uff0c\u53ef\u4ee5\u770b\u5230\u6211\u8c03\u7528\u4e86uploadToWeixinServer\u65b9\u6cd5\uff0c\u53c2\u6570\u4e3a\u672c\u5730\u7167\u7247\u7684Id<\/p>\n<p><\/p>\n<pre>uploadToWeixinServer: 1<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u8c03\u7528uploadImage\u63a5\u53e3\u540e\uff0c\u5c06\u56fe\u7247\u4e0a\u4f20\u5230\u4e86\u5fae\u4fe1\u670d\u52a1\u5668\uff0c\u8fd4\u56de\u56fe\u7247\u7684ID\uff0c\u8fd9\u4e2a\u65f6\u5019\u9700\u8981\u7528ajax\u5f02\u6b65\u4e0a\u4f20\u5230\u81ea\u5df1\u7684\u670d\u52a1\u5668\u91cc\uff0c\u8c03\u7528\u5fae\u4fe1\u63d0\u4f9b\u7684\u201c\u83b7\u53d6\u4e34\u65f6\u7d20\u6750\u201d\u63a5\u53e3\u3002\u5f53\u7136\u4e5f\u4e0d\u4e00\u5b9a\u662f\u9009\u62e9\u5b8c\u7167\u7247\u5c31\u7acb\u5373\u4e0a\u4f20\uff0c\u8fd8\u5f97\u6839\u636e\u5b9e\u9645\u4e1a\u52a1\u9700\u6c42\u51fa\u53d1\uff0c\u4e5f\u6709\u662f\u9759\u9ed8\u4e0a\u4f20\uff08\u6ca1\u6709\u8fdb\u5ea6\u63d0\u793a\uff09\uff0c\u4e5f\u6709\u662f\u5728\u6700\u7ec8\u63d0\u4ea4\u8868\u5355\u65f6\u4e00\u8d77\u4e0a\u4f20<\/p>\n<h3>js:<\/h3>\n<p><\/p>\n<pre>uploadToOwnerServer: function(serverId,type){\r\n            $.ajax({\r\n                data: {serverId:serverId,type:type},\r\n                type : \"POST\",\r\n                url : WX_ROOT + \"wechat\/uploadPhoto\",\r\n                success : function(json) {                    if (json) {                        var data = JSON.parse(json.data);                        if ('car' == type) \r\n                            uploadPhoto.options.carImage = data.path + data.name                        else\r\n                            uploadPhoto.options.idCardImage = data.path + data.name\r\n                        \r\n                    }\r\n                }\r\n            });\r\n        },<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>&nbsp;<\/p>\n<h3>Controller<\/h3>\n<p><\/p>\n<pre>@RequestMapping(value = \"\/uploadPhoto\", method = RequestMethod.POST)    public @ResponseBody HttpResult uploadPhoto(@RequestParam String serverId,@RequestParam String type) throws Exception{\r\n        LOGGER.info(\"RestFul of uploadPhoto parameters serverId:{},type:{}\",serverId,type);        \r\n        try {            \/** \u5c06\u56fe\u7247\u4fdd\u5b58\u5230\u672c\u5730\u670d\u52a1\u5668 **\/\r\n            String photoName = type + new Date().getTime() + UUID.randomUUID().toString();            \r\n            \/\/\u6587\u4ef6\u8def\u5f84\u4e0d\u5b58\u5728\u5219\u521b\u5efa\r\n            File saveFile = new File(PIC_PATH + type);            if (!saveFile.mkdir()) saveFile.mkdir();\r\n            \r\n            wechatService.saveImageToDisk(serverId, photoName, PIC_PATH + type + \"\/\");\r\n            LOGGER.info(\"Download the picture from weixin server pathL:{}\",PIC_PATH + type + \"\/\");\r\n            JSONObject data = new JSONObject();\r\n            data.put(\"name\", type + \"\/\" + photoName+\".jpg\");\r\n            data.put(\"path\", PIC_SERVER + \"\/\");\r\n            \r\n            HttpResult rs = new HttpResult();\r\n            rs.setCode(200);\r\n            rs.setData(data.toJSONString());\r\n            LOGGER.info(\"Download the picture from weixin server is successful!serverId:{},photoName:{}\",serverId,photoName);\r\n            LOGGER.info(\"HttpResult data:{}\",rs.getData());            return rs;\r\n        } catch (Exception e) {\r\n            LOGGER.error(\"Download the picture from weixin server is error\",serverId);            return null;\r\n        }<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u8fd9\u91cc\u6211\u4f7f\u7528\u4e86\u4e00\u4e2aUUID\u751f\u6210\u4e3b\u952e\u89c4\u5219\uff0c\u901a\u8fc7\u7c7b\u578b+\u65f6\u95f4\u6233+\u552f\u4e00\u5b57\u7b26\u4e32\u5b9a\u4e49\u56fe\u7247\u540d\u79f0\u3002\u5982\u679c\u4e0a\u4f20\u6210\u529f\uff0c\u540c\u65f6\u53c8\u5c06\u81ea\u5df1\u670d\u52a1\u5668\u7684\u56fe\u7247\u5730\u5740\u8fd4\u56de\u7ed9\u524d\u7aef\u3002<\/p>\n<p>&nbsp;<\/p>\n<h3>getInputStream<\/h3>\n<p>\u8c03\u7528\u5fae\u4fe1\u63d0\u4f9b\u7684\u83b7\u53d6\u4e34\u65f6\u7d20\u6750\u63a5\u53e3\u4e0b\u8f7d\u8fd8\u5728\u5fae\u4fe1\u670d\u52a1\u5668\u4e0a\u7684\u56fe\u7247\uff0c\u53c2\u6570\u4e3a\u524d\u7aef\u63d0\u4ea4\u4e0a\u6765\u7684\u5a92\u4f53\u6587\u4ef6ID\uff0c\u6700\u7ec8\u5c06\u6587\u4ef6\u8f6c\u5316\u4e3a\u8f93\u5165\u6d41\u5bf9\u8c61<\/p>\n<p><\/p>\n<pre>\/**\r\n     * \u6839\u636e\u6587\u4ef6id\u4e0b\u8f7d\u6587\u4ef6 \r\n     * @param accessToken\r\n     * @param mediaId \r\n     * @return \u6587\u4ef6\u6d41\u5bf9\u8c61     *\/\r\n    public InputStream getInputStream(String accessToken, String mediaId) {  \r\n        InputStream is = null;  \r\n        String url = \"http:\/\/www.php.cn\/\"+ accessToken + \"&amp;media_id=\" + mediaId;  \r\n        try {  \r\n            URL urlGet = new URL(url);  \r\n            HttpURLConnection http = (HttpURLConnection) urlGet.openConnection();  \r\n            http.setRequestMethod(\"GET\"); \/\/ \u5fc5\u987b\u662fget\u65b9\u5f0f\u8bf7\u6c42  \r\n            http.setRequestProperty(\"Content-Type\",\"application\/x-www-form-urlencoded\");  \r\n            http.setDoOutput(true);  \r\n            http.setDoInput(true);  \r\n            System.setProperty(\"sun.net.client.defaultConnectTimeout\", \"30000\");\/\/ \u8fde\u63a5\u8d85\u65f630\u79d2  \r\n            System.setProperty(\"sun.net.client.defaultReadTimeout\", \"30000\"); \/\/ \u8bfb\u53d6\u8d85\u65f630\u79d2              http.connect();  \r\n            \/\/ \u83b7\u53d6\u6587\u4ef6\u8f6c\u5316\u4e3abyte\u6d41  \r\n            is = http.getInputStream();  \r\n        } catch (Exception e) {  \r\n            LOGGER.error(\"Failed to convert inputStream from weixin server,accessToken:{},mediaId:{}\",accessToken,mediaId);\r\n        }  \r\n        return is;  \r\n  \r\n    }<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>&nbsp;<\/p>\n<h3>service<\/h3>\n<p>\u901a\u8fc7\u5faa\u73af\u89e3\u6790\u6d41\u5bf9\u8c61\uff0c\u5c06\u6587\u4ef6\u5199\u5165\u81ea\u5df1\u7684\u670d\u52a1\u5668<\/p>\n<p><\/p>\n<pre>public void saveImageToDisk(String mediaId, String picName, String picPath) throws Exception {  \r\n        String accessToken = getBaseAccessToken();\r\n        InputStream inputStream = getInputStream(accessToken, mediaId); \r\n        \r\n        \/\/ \u5faa\u73af\u53d6\u51fa\u6d41\u4e2d\u7684\u6570\u636e\r\n        byte[] data = new byte[1024];  \r\n        int len = 0;  \r\n        FileOutputStream fileOutputStream = null;  \r\n        try {  \r\n            fileOutputStream = new FileOutputStream(picPath+picName+\".jpg\");  \r\n            while ((len = inputStream.read(data)) != -1) {  \r\n                fileOutputStream.write(data, 0, len);  \r\n            }  \r\n            LOGGER.info(\"Write the fileInputStream is successful\");\r\n        } catch (IOException e) {  \r\n            LOGGER.error(\"Write the fileInputStream is error\");\r\n        } finally {  \r\n            if (inputStream != null) {  \r\n                try {  \r\n                    inputStream.close();  \r\n                } catch (IOException e) {  \r\n                    LOGGER.error(\"Close the fileInputStream is error\");\r\n                }  \r\n            }  \r\n            if (fileOutputStream != null) {  \r\n                try {  \r\n                    fileOutputStream.close();  \r\n                } catch (IOException e) {  \r\n                    LOGGER.error(\"Close the fileOutputStream is error\");\r\n                }  \r\n            }  \r\n        }  \r\n    }<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>&nbsp;<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5fae\u4fe1\u5f00\u53d1\u4e4b\u8c03\u8d77\u6444\u50cf\u5934\u3001\u672c\u5730\u5c55\u793a\u56fe\u7247\u3001\u4e0a\u4f20\u4e0b\u8f7d\u56fe\u7247\u65b9\u6cd5\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u6587\u4ecb\u7ecd\u5fae\u4fe1\u5f00\u53d1\u4e4b\u8c03\u8d77\u6444\u50cf\u5934\u3001\u672c\u5730\u5c55\u793a\u56fe\u7247\u3001\u4e0a\u4f20\u4e0b\u8f7d\u56fe\u7247\u65b9\u6cd5 1.\u914d\u7f6e \u9875\u9762\u5f15\u5165\u901a\u8fc7jssdk\u6388\u6743\u540e\uff0c\u4f20\u5165wx\u5bf9\u8c61\uff0c\u9996\u5148\u914d\u7f6e\u9700\u8981\u7684\u63a5\u53e3 wx.config({ \/* debug: true, *\/ appId: appid, timestamp: timestamp, nonceStr: nonceStr, signature: signature, jsApiList: [ &#8216;chooseImage&#8217;,\/\/\u62cd\u7167\u6216\u4ece\u624b\u673a\u76f8\u518c\u4e2d\u9009\u56fe\u63a5\u53e3 &#8216;previewImage&#8217;,\/\/\u9884\u89c8\u56fe\u7247\u63a5\u53e3 &#8216;uploadImage&#8217;,\/\/\u4e0a\u4f20\u56fe\u7247\u63a5\u53e3 &#8216;downloadImage&#8217;\/\/\u4e0b\u8f7d\u56fe\u7247\u63a5\u53e3 \u3000\u3000] }); \u767b\u5f55\u540e\u590d\u5236 &nbsp; 2.\u8c03\u8d77\u62cd\u7167\/\u76f8\u518c \u5c06\u4e0b\u9762\u7684\u65b9\u6cd5\u653e\u5728\u9700\u8981\u70b9\u51fb\u4e8b\u4ef6\u7684\u56de\u8c03\u51fd\u6570\u91cc\u9762&nbsp; wx.chooseImage({ count: 1, sizeType: [&#8216;compressed&#8217;], sourceType: [&#8216;album&#8217;, &#8216;camera&#8217;], success: function (res) { \u3000\u3000\u3000\u3000\/\/var localIds = res.localIds; \u3000 $(&#8216;.driver-card img&#8217;).prop(&#8216;src&#8217;,res.localIds[0]); \u3000 uploadPhoto.uploadToWeixinServer(res.localIds[0],&#8217;car&#8217;) } }); \u767b\u5f55\u540e\u590d\u5236 &nbsp; \u8fd9\u65f6\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u8fd9\u6837\u7684\u6548\u679c\uff0c\u4ee3\u8868\u8c03\u8d77\u6210\u529f\u4e86\uff01chooseImage\u65b9\u6cd5\u7684\u6210\u529f\u56de\u8c03\u91cc\uff0c\u6211\u5c06\u9009\u4e2d\u7684\u7167\u7247\u8d4b\u503c\u7ed9\u9700\u8981\u663e\u793a\u7684img\u7684src\uff08\u56e0\u4e3a\u6211\u8fd9\u91cc\u53ea\u6709\u4e00\u5f20\u7167\u7247\uff0c\u5982\u679c\u6709\u591a\u5f20\u7528\u5faa\u73af\u8d4b\u503c\u5373\u53ef\uff09\uff0c\u8fd9\u6837\u4e00\u6765\uff0c\u5c31\u53ef\u4ee5\u76f4\u63a5\u663e\u793a\u521a\u521a\u62cd\u7167\/\u76f8\u518c\u91cc\u9009\u4e2d\u7684\u7167\u7247\u4e86 &nbsp; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19],"tags":[],"class_list":["post-28810","post","type-post","status-publish","format-standard","hentry","category-19"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/28810","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=28810"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/28810\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=28810"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=28810"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=28810"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}