├── .gitignore ├── LICENSE ├── README.md ├── pom.xml └── src └── main └── java └── com └── bushangbuxia ├── App.java └── imagehosting ├── DefaultImageHostingService.java ├── ImageHostingService.java ├── constants ├── ImageHostingPlatform.java └── ImageSuffix.java ├── domain ├── ImageHostingOptions.java ├── ImageHostingReqConfig.java └── ImageHostingResponse.java ├── service ├── AbstractInnerImageUploadService.java ├── AliexpressImageUploadService.java ├── BilibiliUploadService.java ├── BytedanceImageUploadService.java ├── CatBoxMoreService.java ├── CcImageUploadService.java ├── FreeImageHostImageService.java ├── InnerImageUploadService.java ├── JdImageUploadService.java ├── JueJinImageUploadService.java ├── NeteaseImageUploadService.java ├── PrntscrImageUploadService.java ├── SouhuImageUploadService.java ├── SuNingImageUploadService.java └── VimCNImageService.java └── utils └── RandomUtils.java /.gitignore: -------------------------------------------------------------------------------- 1 | # maven ignore 2 | target/ 3 | *.jar 4 | *.war 5 | *.zip 6 | *.tar 7 | *.tar.gz 8 | 9 | 10 | # eclipse ignore 11 | .settings/ 12 | .project 13 | .classpath 14 | *.class 15 | 16 | 17 | # idea ignore 18 | .idea/ 19 | *.ipr 20 | *.iml 21 | *.iws 22 | 23 | # temp ignore 24 | *.log 25 | *.cache 26 | *.diff 27 | *.patch 28 | *.tmp 29 | 30 | 31 | */bin/* 32 | # system ignore 33 | .DS_Store 34 | Thumbs.db 35 | /bin/ 36 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 老夫正年轻 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ### Introduction 2 | 3 | ImageHosting是一个集合多家免费图床的JAVA实现 4 | 5 | ### Guides 6 | 7 | 1、添加依赖 8 | 9 | ``` 10 | 11 | com.bushangbuxia 12 | imagehosting 13 | 1.0.0 14 | 15 | ``` 16 | 最新版本:[https://search.maven.org/artifact/com.bushangbuxia/imagehosting](https://search.maven.org/artifact/com.bushangbuxia/imagehosting) 17 | 18 | 2、使用案例 19 | 20 | ```java 21 | public static void main(String[] args) { 22 | try { 23 | ImageHostingService imageHostingService = new DefaultImageHostingService(); 24 | File imageFile = new File("C:\\5E44465978FB976E3597D3110CB76AB9.png"); 25 | ImageHostingOptions options = new ImageHostingOptions(); 26 | options.setHostingPlatforms(Arrays.asList(ImageHostingPlatform.JD)); 27 | ImageHostingResponse response = imageHostingService.upload(imageFile, options); 28 | System.out.println(JSON.toJSONString(response)); 29 | } catch (Exception e) { 30 | e.printStackTrace(); 31 | } 32 | } 33 | ``` 34 | 35 | ### Support Site 36 | 37 | 当前支持的站点 38 | 39 | - 苏宁 40 | - 京东 41 | - 阿里 42 | - 今日头条 43 | - 网易 44 | - 搜狐 45 | - CC 46 | - 掘金 47 | 48 | 相关站点正在陆续加入中,欢迎贡献pull request...... 49 | 50 | ### License 51 | 52 | ImageHosting is released under the [MIT license](https://github.com/jingxiang/imagehosting/blob/master/LICENSE). 53 | 54 | 55 | 56 | 57 | 58 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 4 | 5 | com.kalman03 6 | kalman03-parent 7 | 1.0.1-SNAPSHOT 8 | 9 | 4.0.0 10 | com.bushangbuxia 11 | imagehosting 12 | jar 13 | 14 | 1.0.1-SNAPSHOT 15 | imagehosting 16 | ImageHosting是一个集合多家免费图床的JAVA实现 17 | https://github.com/jingxiang/imagehosting 18 | 19 | 20 | https://github.com/jingxiang/imagehosting.git 21 | 22 | 23 | 24 | 25 | MIT License 26 | https://github.com/jingxiang/imagehosting/blob/master/LICENSE 27 | 28 | 29 | 30 | 31 | 32 | kalman03 33 | kalman03@163.com 34 | bushangbuxia 35 | http://www.bushangbuxia.com 36 | 37 | 38 | 39 | 40 | 1.8 41 | UTF-8 42 | ${java.version} 43 | ${java.version} 44 | 45 | 46 | 47 | 48 | com.alibaba 49 | fastjson 50 | 51 | 52 | org.projectlombok 53 | lombok 54 | true 55 | 56 | 57 | org.apache.httpcomponents 58 | httpclient 59 | 60 | 61 | org.apache.httpcomponents 62 | httpmime 63 | 64 | 65 | commons-logging 66 | commons-logging 67 | 68 | 69 | 70 | 71 | 72 | kalman03-rdc-releases 73 | https://packages.aliyun.com/maven/repository/2029728-release-uPq0hR/ 74 | 75 | 76 | kalman03-rdc-snapshots 77 | https://packages.aliyun.com/maven/repository/2029728-snapshot-1jKfF4/ 78 | 79 | 80 | 81 | 82 | ${project.name} 83 | 84 | 85 | org.apache.maven.plugins 86 | maven-compiler-plugin 87 | 88 | ${java.version} 89 | ${java.version} 90 | ${project.build.sourceEncoding} 91 | 92 | 93 | 94 | org.apache.maven.plugins 95 | maven-source-plugin 96 | 97 | true 98 | 99 | 100 | 101 | attach-sources 102 | verify 103 | 104 | jar-no-fork 105 | 106 | 107 | 108 | 109 | 110 | org.apache.maven.plugins 111 | maven-javadoc-plugin 112 | 3.0.1 113 | 114 | 115 | package 116 | 117 | jar 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | -------------------------------------------------------------------------------- /src/main/java/com/bushangbuxia/App.java: -------------------------------------------------------------------------------- 1 | package com.bushangbuxia; 2 | 3 | import java.io.File; 4 | import java.util.Arrays; 5 | 6 | import com.alibaba.fastjson.JSON; 7 | import com.bushangbuxia.imagehosting.DefaultImageHostingService; 8 | import com.bushangbuxia.imagehosting.ImageHostingService; 9 | import com.bushangbuxia.imagehosting.constants.ImageHostingPlatform; 10 | import com.bushangbuxia.imagehosting.domain.ImageHostingOptions; 11 | import com.bushangbuxia.imagehosting.domain.ImageHostingResponse; 12 | 13 | /** 14 | * Hello world! 15 | */ 16 | public class App { 17 | public static void main(String[] args) { 18 | try { 19 | ImageHostingService imageHostingService = new DefaultImageHostingService(); 20 | File imageFile = new File("C:\\Users\\kalman\\Desktop\\微信截图_20210719224153.png"); 21 | ImageHostingOptions options = new ImageHostingOptions(); 22 | options.setHostingPlatforms(Arrays.asList(ImageHostingPlatform.FREE_IMAGE_HOST)); 23 | ImageHostingResponse response = imageHostingService.upload(imageFile, options); 24 | System.out.println(JSON.toJSONString(response)); 25 | } catch (Exception e) { 26 | e.printStackTrace(); 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/com/bushangbuxia/imagehosting/DefaultImageHostingService.java: -------------------------------------------------------------------------------- 1 | package com.bushangbuxia.imagehosting; 2 | 3 | import java.awt.Image; 4 | import java.io.BufferedOutputStream; 5 | import java.io.File; 6 | import java.io.FileOutputStream; 7 | import java.io.IOException; 8 | 9 | import javax.imageio.ImageIO; 10 | 11 | import com.bushangbuxia.imagehosting.constants.ImageHostingPlatform; 12 | import com.bushangbuxia.imagehosting.domain.ImageHostingOptions; 13 | import com.bushangbuxia.imagehosting.domain.ImageHostingResponse; 14 | import com.bushangbuxia.imagehosting.domain.ImageHostingResponse.ImageDO; 15 | import com.bushangbuxia.imagehosting.service.AliexpressImageUploadService; 16 | import com.bushangbuxia.imagehosting.service.BilibiliUploadService; 17 | import com.bushangbuxia.imagehosting.service.BytedanceImageUploadService; 18 | import com.bushangbuxia.imagehosting.service.CatBoxMoreService; 19 | import com.bushangbuxia.imagehosting.service.CcImageUploadService; 20 | import com.bushangbuxia.imagehosting.service.FreeImageHostImageService; 21 | import com.bushangbuxia.imagehosting.service.InnerImageUploadService; 22 | import com.bushangbuxia.imagehosting.service.JdImageUploadService; 23 | import com.bushangbuxia.imagehosting.service.JueJinImageUploadService; 24 | import com.bushangbuxia.imagehosting.service.NeteaseImageUploadService; 25 | import com.bushangbuxia.imagehosting.service.SouhuImageUploadService; 26 | import com.bushangbuxia.imagehosting.service.SuNingImageUploadService; 27 | import com.bushangbuxia.imagehosting.service.VimCNImageService; 28 | import com.bushangbuxia.imagehosting.utils.RandomUtils; 29 | 30 | /** 31 | * @author kalman03 32 | * @since 2019-08-17 33 | */ 34 | public class DefaultImageHostingService implements ImageHostingService { 35 | 36 | public ImageHostingResponse upload(byte[] imageBytes, ImageHostingOptions options) throws IOException { 37 | String prefix = RandomUtils.randomString(7); 38 | File imageFile = File.createTempFile(prefix, options.getSuffix().getValue()); 39 | byte2File(imageBytes, imageFile); 40 | return upload(imageFile, options); 41 | } 42 | 43 | public ImageHostingResponse upload(File imageFile, ImageHostingOptions options) throws IOException { 44 | if (imageFile == null) { 45 | return error("请指定上传图片的内容"); 46 | } 47 | if (options.getHostingPlatforms() == null || options.getHostingPlatforms().isEmpty()) { 48 | return error("hostingPlatforms参数不能为空,至少指定一个图床站点"); 49 | } 50 | for (int hostingPlatform : options.getHostingPlatforms()) { 51 | if (!ImageHostingPlatform.ALL_HOSTING_PLATFORMS.contains(hostingPlatform)) { 52 | return error("hostingPlatforms参数中存在不支持的图传站点"); 53 | } 54 | } 55 | if (options.isCheckImage() && isImage(imageFile)) { 56 | return error("上传的图片损坏或非图片文件"); 57 | } 58 | for (int hostingPlatform : options.getHostingPlatforms()) { 59 | try { 60 | InnerImageUploadService innerImageUploadService = adapterUploadService(hostingPlatform); 61 | String absoluteUrl = innerImageUploadService.upload(imageFile, options.getReqConfig()); 62 | if (absoluteUrl != null && !absoluteUrl.isEmpty()) { 63 | return success(absoluteUrl, hostingPlatform); 64 | } 65 | } catch (Exception e) { 66 | } 67 | } 68 | return error("上传图片失败 "); 69 | } 70 | 71 | private InnerImageUploadService adapterUploadService(int hostingPlatform) { 72 | if (hostingPlatform == ImageHostingPlatform.SUNING) { 73 | return new SuNingImageUploadService(); 74 | } else if (hostingPlatform == ImageHostingPlatform.ALIEXPRESS) { 75 | return new AliexpressImageUploadService(); 76 | } else if (hostingPlatform == ImageHostingPlatform.JD) { 77 | return new JdImageUploadService(); 78 | } else if (hostingPlatform == ImageHostingPlatform.BYTEDANCE) { 79 | return new BytedanceImageUploadService(); 80 | } else if (hostingPlatform == ImageHostingPlatform.SOUHU) { 81 | return new SouhuImageUploadService(); 82 | } else if (hostingPlatform == ImageHostingPlatform.NETEASE) { 83 | return new NeteaseImageUploadService(); 84 | } else if (hostingPlatform == ImageHostingPlatform.CC) { 85 | return new CcImageUploadService(); 86 | } else if (hostingPlatform == ImageHostingPlatform.JUEJIN) { 87 | return new JueJinImageUploadService(); 88 | // return new PrntscrImageUploadService(); 89 | } else if (hostingPlatform == ImageHostingPlatform.BILIBILI) { 90 | return new BilibiliUploadService(); 91 | } else if (hostingPlatform == ImageHostingPlatform.CATBOX) { 92 | return new CatBoxMoreService(); 93 | } else if (hostingPlatform == ImageHostingPlatform.VIM_CN) { 94 | return new VimCNImageService(); 95 | } else if (hostingPlatform == ImageHostingPlatform.FREE_IMAGE_HOST) { 96 | return new FreeImageHostImageService(); 97 | } 98 | return null; 99 | } 100 | 101 | private ImageHostingResponse error(String message) { 102 | ImageHostingResponse response = new ImageHostingResponse(); 103 | response.setMessage(message); 104 | response.setSuccess(false); 105 | return response; 106 | } 107 | 108 | private ImageHostingResponse success(String absoluteUrl, int platform) { 109 | ImageHostingResponse response = new ImageHostingResponse(); 110 | response.setMessage("上传成功"); 111 | response.setSuccess(true); 112 | ImageDO imageDO = new ImageDO(); 113 | imageDO.setAbsoluteUrl(absoluteUrl); 114 | imageDO.setPlatform(platform); 115 | response.setImageDO(imageDO); 116 | return response; 117 | } 118 | 119 | private boolean isImage(File imageFile) { 120 | try { 121 | Image image = ImageIO.read(imageFile); 122 | return image != null && image.getWidth(null) > 0 && image.getHeight(null) > 0; 123 | } catch (Exception e) { 124 | // ignore 125 | } 126 | return false; 127 | } 128 | 129 | private void byte2File(byte[] buf, File file) { 130 | BufferedOutputStream bos = null; 131 | FileOutputStream fos = null; 132 | try { 133 | fos = new FileOutputStream(file); 134 | bos = new BufferedOutputStream(fos); 135 | bos.write(buf); 136 | } catch (Exception e) { 137 | // ignore 138 | } finally { 139 | try { 140 | if (bos != null) { 141 | bos.close(); 142 | fos.close(); 143 | } 144 | } catch (IOException e) { 145 | } 146 | } 147 | } 148 | } 149 | -------------------------------------------------------------------------------- /src/main/java/com/bushangbuxia/imagehosting/ImageHostingService.java: -------------------------------------------------------------------------------- 1 | package com.bushangbuxia.imagehosting; 2 | 3 | import java.io.File; 4 | import java.io.IOException; 5 | 6 | import com.bushangbuxia.imagehosting.domain.ImageHostingOptions; 7 | import com.bushangbuxia.imagehosting.domain.ImageHostingResponse; 8 | 9 | /** 10 | * @author kalman03 11 | * @since 2019-08-17 12 | */ 13 | public interface ImageHostingService { 14 | 15 | ImageHostingResponse upload(byte[] imageBytes, ImageHostingOptions options)throws IOException; 16 | 17 | ImageHostingResponse upload(File imageFile, ImageHostingOptions options)throws IOException; 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/com/bushangbuxia/imagehosting/constants/ImageHostingPlatform.java: -------------------------------------------------------------------------------- 1 | package com.bushangbuxia.imagehosting.constants; 2 | 3 | import java.util.Arrays; 4 | import java.util.List; 5 | 6 | /** 7 | * 图床站点 8 | * 9 | * @author kalman03 10 | * @since 2019-08-17 11 | */ 12 | public class ImageHostingPlatform { 13 | /** 14 | * 苏宁 15 | */ 16 | public final static int SUNING = 1; 17 | /** 18 | * aliexpress ,在微信里面可能无法直接打开 19 | */ 20 | public final static int ALIEXPRESS = 2; 21 | /** 22 | * 京东,不支持尺寸太小的图片 23 | */ 24 | public final static int JD = 3; 25 | /** 26 | * 今日头条 27 | */ 28 | public final static int BYTEDANCE = 4; 29 | /** 30 | * 搜狐 31 | */ 32 | public final static int SOUHU = 5; 33 | /** 34 | * 网易 35 | */ 36 | public final static int NETEASE = 6; 37 | /** 38 | * CC 39 | */ 40 | public final static int CC = 7; 41 | /** 42 | * 掘金 43 | */ 44 | public final static int JUEJIN = 8; 45 | /** 46 | * B站 47 | */ 48 | public final static int BILIBILI = 9; 49 | /** 50 | * catbox.moe 51 | */ 52 | public final static int CATBOX = 10; 53 | /** 54 | * img.vim-cn.com 55 | */ 56 | public final static int VIM_CN = 11; 57 | /** 58 | * img.vim-cn.com 59 | */ 60 | public final static int FREE_IMAGE_HOST = 12; 61 | 62 | public final static List ALL_HOSTING_PLATFORMS = Arrays.asList(JUEJIN, CC, NETEASE, SUNING, ALIEXPRESS, JD, 63 | BYTEDANCE, SOUHU, BILIBILI, CATBOX, VIM_CN, FREE_IMAGE_HOST); 64 | } 65 | -------------------------------------------------------------------------------- /src/main/java/com/bushangbuxia/imagehosting/constants/ImageSuffix.java: -------------------------------------------------------------------------------- 1 | package com.bushangbuxia.imagehosting.constants; 2 | /** 3 | * @author kalman03 4 | * @since 2019-08-17 5 | */ 6 | public enum ImageSuffix { 7 | 8 | PNG(".png"),JPG(".jpg"); 9 | 10 | private ImageSuffix(String value) { 11 | this.value = value; 12 | } 13 | 14 | private String value; 15 | 16 | public String getValue() { 17 | return value; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/com/bushangbuxia/imagehosting/domain/ImageHostingOptions.java: -------------------------------------------------------------------------------- 1 | package com.bushangbuxia.imagehosting.domain; 2 | 3 | import java.util.Arrays; 4 | import java.util.List; 5 | 6 | import com.bushangbuxia.imagehosting.constants.ImageHostingPlatform; 7 | import com.bushangbuxia.imagehosting.constants.ImageSuffix; 8 | 9 | import lombok.Data; 10 | 11 | /** 12 | * @author kalman03 13 | * @since 2019-08-17 14 | */ 15 | @Data 16 | public class ImageHostingOptions { 17 | /** 18 | * 指定上传图片的后缀,请与上传图片类型对应。该参数仅对上传参数传byte[]时有效。若不指定,默认为png,部分图床站点遇到图片类型与后缀不一致,会报错。 19 | */ 20 | private ImageSuffix suffix; 21 | /** 22 | * 指定要上传图片所在图床的站点,遇到一个站点上传成功则立即返回,否则继续下一个站点。建议list长度不要超过2个站点。 23 | * @see ImageHostingPlatform 24 | */ 25 | private List hostingPlatforms = Arrays.asList(ImageHostingPlatform.SUNING, ImageHostingPlatform.ALIEXPRESS); 26 | /** 27 | * 是否需要校验上传文件是否为图片,若前置业务已校验,可忽略 28 | */ 29 | private boolean checkImage = false; 30 | /** 31 | * 上传超时配置 32 | */ 33 | private ImageHostingReqConfig reqConfig = new ImageHostingReqConfig(); 34 | } 35 | -------------------------------------------------------------------------------- /src/main/java/com/bushangbuxia/imagehosting/domain/ImageHostingReqConfig.java: -------------------------------------------------------------------------------- 1 | package com.bushangbuxia.imagehosting.domain; 2 | 3 | import lombok.Data; 4 | 5 | /** 6 | * @author kalman03 7 | * @since 2019-08-17 8 | */ 9 | @Data 10 | public class ImageHostingReqConfig { 11 | 12 | private int connetionTimeOut = 1000*3; 13 | 14 | private int socketTimeout = 1000*3; 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/com/bushangbuxia/imagehosting/domain/ImageHostingResponse.java: -------------------------------------------------------------------------------- 1 | package com.bushangbuxia.imagehosting.domain; 2 | 3 | import lombok.Data; 4 | 5 | @Data 6 | public class ImageHostingResponse { 7 | 8 | private ImageDO imageDO; 9 | private boolean success; 10 | private String message; 11 | 12 | @Data 13 | public static class ImageDO{ 14 | private String absoluteUrl; 15 | private int platform; 16 | } 17 | } -------------------------------------------------------------------------------- /src/main/java/com/bushangbuxia/imagehosting/service/AbstractInnerImageUploadService.java: -------------------------------------------------------------------------------- 1 | package com.bushangbuxia.imagehosting.service; 2 | 3 | import java.io.File; 4 | import java.io.IOException; 5 | import java.util.Map; 6 | 7 | import org.apache.http.HttpEntity; 8 | import org.apache.http.client.config.RequestConfig; 9 | import org.apache.http.client.methods.CloseableHttpResponse; 10 | import org.apache.http.client.methods.HttpPost; 11 | import org.apache.http.impl.client.CloseableHttpClient; 12 | import org.apache.http.impl.client.HttpClients; 13 | import org.apache.http.message.BasicHeader; 14 | import org.apache.http.util.EntityUtils; 15 | 16 | import com.bushangbuxia.imagehosting.domain.ImageHostingReqConfig; 17 | 18 | /** 19 | * @author kalman03 20 | * @since 2019-08-17 21 | */ 22 | public abstract class AbstractInnerImageUploadService implements InnerImageUploadService { 23 | 24 | abstract String getUploadUrl(); 25 | 26 | abstract HttpEntity getHttpEntity(File imageFile); 27 | 28 | abstract String anaysisResult(String response); 29 | 30 | abstract Map getHeaderMap(); 31 | 32 | public String upload(File imageFile,ImageHostingReqConfig reqConfig) throws IOException { 33 | String result = null; 34 | CloseableHttpClient httpclient = HttpClients.createDefault(); 35 | String url = getUploadUrl(); 36 | HttpPost httpPost = new HttpPost(url); 37 | RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(reqConfig.getConnetionTimeOut()) 38 | .setSocketTimeout(reqConfig.getSocketTimeout()).build(); 39 | httpPost.setConfig(requestConfig); 40 | httpPost.setEntity(getHttpEntity(imageFile)); 41 | Map headerMap = getHeaderMap(); 42 | if(headerMap != null && !headerMap.isEmpty()) { 43 | for(Map.Entry entry : headerMap.entrySet()) { 44 | httpPost.addHeader(new BasicHeader(entry.getKey(), entry.getValue())); 45 | } 46 | } 47 | CloseableHttpResponse response = httpclient.execute(httpPost); 48 | try { 49 | HttpEntity resEntity = response.getEntity(); 50 | if (resEntity != null) { 51 | String responseEntity = EntityUtils.toString(response.getEntity()); 52 | result = anaysisResult(responseEntity); 53 | if(result == null || result.isEmpty()) { 54 | printError(url, responseEntity); 55 | } 56 | } 57 | 58 | // EntityUtils.consume(resEntity); 59 | return result; 60 | } finally { 61 | try { 62 | response.close(); 63 | } catch (Exception e) { 64 | } 65 | } 66 | } 67 | 68 | private void printError(String url,String responseEntity) { 69 | System.out.println("上传图片失败,返回结果------>:\n==============="+responseEntity+"===============\n请求站点:"+url); 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /src/main/java/com/bushangbuxia/imagehosting/service/AliexpressImageUploadService.java: -------------------------------------------------------------------------------- 1 | package com.bushangbuxia.imagehosting.service; 2 | 3 | import java.io.File; 4 | import java.util.HashMap; 5 | import java.util.Map; 6 | 7 | import org.apache.http.HttpEntity; 8 | import org.apache.http.entity.ContentType; 9 | import org.apache.http.entity.mime.MultipartEntityBuilder; 10 | import org.apache.http.entity.mime.content.StringBody; 11 | 12 | import com.alibaba.fastjson.JSON; 13 | import com.alibaba.fastjson.JSONObject; 14 | import com.bushangbuxia.imagehosting.utils.RandomUtils; 15 | 16 | /** 17 | * @author kalman03 18 | * @since 2019-08-17 19 | */ 20 | public class AliexpressImageUploadService extends AbstractInnerImageUploadService { 21 | 22 | @Override 23 | String getUploadUrl() { 24 | return "https://kfupload.alibaba.com/mupload"; 25 | } 26 | 27 | @Override 28 | HttpEntity getHttpEntity(File imageFile) { 29 | String suffix = imageFile.getName().substring(imageFile.getName().lastIndexOf(".")); 30 | return MultipartEntityBuilder.create().addBinaryBody("file", imageFile) 31 | .addPart("scene", new StringBody("aeMessageCenterV2ImageRule", ContentType.DEFAULT_TEXT)) 32 | .addPart("name", new StringBody(RandomUtils.randomString(7) + suffix, ContentType.DEFAULT_TEXT)) 33 | .build(); 34 | } 35 | 36 | @Override 37 | String anaysisResult(String response) { 38 | JSONObject jsonObject = JSON.parseObject(response); 39 | if (jsonObject.getIntValue("code") == 0) { 40 | return "https://ae01.alicdn.com/kf/"+jsonObject.getString("fs_url"); 41 | } 42 | return null; 43 | } 44 | 45 | @Override 46 | Map getHeaderMap() { 47 | Map map = new HashMap(); 48 | map.put("User-Agent", "iAliexpress/6.22.1 (iPhone; iOS 12.1.2; Scale/2.00)"); 49 | return map; 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/main/java/com/bushangbuxia/imagehosting/service/BilibiliUploadService.java: -------------------------------------------------------------------------------- 1 | package com.bushangbuxia.imagehosting.service; 2 | 3 | import java.io.File; 4 | import java.util.HashMap; 5 | import java.util.Map; 6 | 7 | import org.apache.http.HttpEntity; 8 | import org.apache.http.entity.ContentType; 9 | import org.apache.http.entity.mime.MultipartEntityBuilder; 10 | import org.apache.http.entity.mime.content.StringBody; 11 | 12 | import com.alibaba.fastjson.JSON; 13 | 14 | /** 15 | * @author kalman03 16 | * @since 2019-08-17 17 | */ 18 | public class BilibiliUploadService extends AbstractInnerImageUploadService { 19 | 20 | @Override 21 | String getUploadUrl() { 22 | return "https://api.vc.bilibili.com/api/v1/drawImage/upload"; 23 | } 24 | 25 | @Override 26 | HttpEntity getHttpEntity(File imageFile) { 27 | return MultipartEntityBuilder.create().addBinaryBody("file_up", imageFile) 28 | .addPart("category", new StringBody("daily", ContentType.DEFAULT_TEXT)) 29 | .addPart("biz", new StringBody("draw", ContentType.DEFAULT_TEXT)).build(); 30 | } 31 | 32 | @Override 33 | String anaysisResult(String response) { 34 | try { 35 | System.out.println(response); 36 | //{"code":0,"message":"success","data":{"image_url":"http:\/\/i0.hdslb.com\/bfs\/album\/8eac2cf7ced7f3b5db455e22dc9c930f5c09967b.png","image_width":2442,"image_height":873}} 37 | return JSON.parseObject(response).getJSONObject("data").getString("image_url"); 38 | } catch (Exception e) { 39 | // ignore 40 | } 41 | return null; 42 | } 43 | 44 | @Override 45 | Map getHeaderMap() { 46 | Map map = new HashMap<>(); 47 | map.put("Cookie", "SESSDATA=b1a03641%2C1642516858%2C8fcba%2A71"); 48 | return map; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/main/java/com/bushangbuxia/imagehosting/service/BytedanceImageUploadService.java: -------------------------------------------------------------------------------- 1 | package com.bushangbuxia.imagehosting.service; 2 | 3 | import java.io.File; 4 | import java.util.HashMap; 5 | import java.util.Map; 6 | 7 | import org.apache.http.HttpEntity; 8 | import org.apache.http.entity.mime.MultipartEntityBuilder; 9 | 10 | import com.alibaba.fastjson.JSON; 11 | import com.alibaba.fastjson.JSONObject; 12 | 13 | /** 14 | * @author kalman03 15 | * @since 2019-08-17 16 | */ 17 | public class BytedanceImageUploadService extends AbstractInnerImageUploadService { 18 | 19 | @Override 20 | String getUploadUrl() { 21 | return "https://mp.toutiao.com/upload_photo/?type=json"; 22 | } 23 | 24 | @Override 25 | HttpEntity getHttpEntity(File imageFile) { 26 | return MultipartEntityBuilder.create().addBinaryBody("photo", imageFile).build(); 27 | } 28 | 29 | @Override 30 | String anaysisResult(String response) { 31 | JSONObject jsonObject = JSON.parseObject(response); 32 | return jsonObject.getString("web_url"); 33 | } 34 | 35 | @Override 36 | Map getHeaderMap() { 37 | return new HashMap(); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/main/java/com/bushangbuxia/imagehosting/service/CatBoxMoreService.java: -------------------------------------------------------------------------------- 1 | package com.bushangbuxia.imagehosting.service; 2 | 3 | import java.io.File; 4 | import java.util.HashMap; 5 | import java.util.Map; 6 | 7 | import org.apache.http.HttpEntity; 8 | import org.apache.http.entity.ContentType; 9 | import org.apache.http.entity.mime.MultipartEntityBuilder; 10 | import org.apache.http.entity.mime.content.StringBody; 11 | 12 | /** 13 | * @author kalman03 14 | * @since 2019-08-17 15 | */ 16 | public class CatBoxMoreService extends AbstractInnerImageUploadService { 17 | 18 | @Override 19 | String getUploadUrl() { 20 | return "https://catbox.moe/user/api.php"; 21 | } 22 | 23 | @Override 24 | HttpEntity getHttpEntity(File imageFile) { 25 | return MultipartEntityBuilder.create().addBinaryBody("fileToUpload", imageFile) 26 | .addPart("reqtype", new StringBody("fileupload", ContentType.DEFAULT_TEXT)) 27 | .build(); 28 | } 29 | 30 | @Override 31 | String anaysisResult(String response) { 32 | try { 33 | System.out.println(response); 34 | return response.trim(); 35 | } catch (Exception e) { 36 | // ignore 37 | } 38 | return null; 39 | } 40 | 41 | @Override 42 | Map getHeaderMap() { 43 | Map map = new HashMap<>(); 44 | return map; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/com/bushangbuxia/imagehosting/service/CcImageUploadService.java: -------------------------------------------------------------------------------- 1 | package com.bushangbuxia.imagehosting.service; 2 | 3 | import java.io.File; 4 | import java.util.HashMap; 5 | import java.util.Map; 6 | 7 | import org.apache.http.HttpEntity; 8 | import org.apache.http.entity.mime.MultipartEntityBuilder; 9 | 10 | import com.alibaba.fastjson.JSON; 11 | 12 | /** 13 | * @author kalman03 14 | * @since 2019-08-17 15 | */ 16 | public class CcImageUploadService extends AbstractInnerImageUploadService { 17 | 18 | @Override 19 | String getUploadUrl() { 20 | return "https://upload.cc/image_upload"; 21 | } 22 | 23 | @Override 24 | HttpEntity getHttpEntity(File imageFile) { 25 | return MultipartEntityBuilder.create().addBinaryBody("uploaded_file[]", imageFile).build(); 26 | } 27 | 28 | @Override 29 | String anaysisResult(String response) { 30 | try { 31 | return "https://upload.cc/" 32 | + JSON.parseObject(response).getJSONArray("success_image").getJSONObject(0).getString("url"); 33 | } catch (Exception e) { 34 | // ignore 35 | } 36 | return null; 37 | } 38 | 39 | @Override 40 | Map getHeaderMap() { 41 | return new HashMap(); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/main/java/com/bushangbuxia/imagehosting/service/FreeImageHostImageService.java: -------------------------------------------------------------------------------- 1 | package com.bushangbuxia.imagehosting.service; 2 | 3 | import java.io.File; 4 | import java.util.HashMap; 5 | import java.util.Map; 6 | 7 | import org.apache.http.HttpEntity; 8 | import org.apache.http.entity.ContentType; 9 | import org.apache.http.entity.mime.MultipartEntityBuilder; 10 | import org.apache.http.entity.mime.content.StringBody; 11 | 12 | import com.alibaba.fastjson.JSON; 13 | 14 | /** 15 | * @author kalman03 16 | * @since 2019-08-17 17 | */ 18 | public class FreeImageHostImageService extends AbstractInnerImageUploadService { 19 | 20 | @Override 21 | String getUploadUrl() { 22 | return "https://freeimage.host/json"; 23 | } 24 | 25 | @Override 26 | HttpEntity getHttpEntity(File imageFile) { 27 | return MultipartEntityBuilder.create().addBinaryBody("source", imageFile) 28 | .addPart("type", new StringBody("file", ContentType.DEFAULT_TEXT)) 29 | .addPart("action", new StringBody("upload", ContentType.DEFAULT_TEXT)) 30 | .build(); 31 | } 32 | 33 | @Override 34 | String anaysisResult(String response) { 35 | try { 36 | System.out.println(response); 37 | return JSON.parseObject(response).getJSONObject("image").getJSONObject("image").getString("url"); 38 | } catch (Exception e) { 39 | // ignore 40 | } 41 | return null; 42 | } 43 | 44 | @Override 45 | Map getHeaderMap() { 46 | Map map = new HashMap<>(); 47 | return map; 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src/main/java/com/bushangbuxia/imagehosting/service/InnerImageUploadService.java: -------------------------------------------------------------------------------- 1 | package com.bushangbuxia.imagehosting.service; 2 | /** 3 | * @author kalman03 4 | * @since 2019-08-17 5 | */ 6 | 7 | import java.io.File; 8 | import java.io.IOException; 9 | 10 | import com.bushangbuxia.imagehosting.domain.ImageHostingReqConfig; 11 | 12 | public interface InnerImageUploadService { 13 | 14 | String upload(File imageFile,ImageHostingReqConfig reqConfig)throws IOException; 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/com/bushangbuxia/imagehosting/service/JdImageUploadService.java: -------------------------------------------------------------------------------- 1 | package com.bushangbuxia.imagehosting.service; 2 | 3 | import java.io.File; 4 | import java.util.HashMap; 5 | import java.util.Map; 6 | import java.util.regex.Matcher; 7 | import java.util.regex.Pattern; 8 | 9 | import org.apache.http.HttpEntity; 10 | import org.apache.http.entity.mime.MultipartEntityBuilder; 11 | 12 | /** 13 | * @author kalman03 14 | * @since 2019-08-17 15 | */ 16 | public class JdImageUploadService extends AbstractInnerImageUploadService { 17 | 18 | @Override 19 | String getUploadUrl() { 20 | return "https://search.jd.com/image?op=upload"; 21 | } 22 | 23 | @Override 24 | HttpEntity getHttpEntity(File imageFile) { 25 | return MultipartEntityBuilder.create().addBinaryBody("file", imageFile).build(); 26 | } 27 | 28 | @Override 29 | String anaysisResult(String response) { 30 | if (response.contains("callback")) { 31 | Pattern p = Pattern.compile("(?m)\\(\"(.*)\"\\)"); 32 | Matcher m = p.matcher(response); 33 | String temp = ""; 34 | while (m.find()) { 35 | temp = m.group(); 36 | } 37 | temp = temp.substring(2, temp.length() - 2); 38 | return "https://img1.360buyimg.com/img/" + temp; 39 | } 40 | return null; 41 | } 42 | 43 | @Override 44 | Map getHeaderMap() { 45 | return new HashMap(); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/main/java/com/bushangbuxia/imagehosting/service/JueJinImageUploadService.java: -------------------------------------------------------------------------------- 1 | package com.bushangbuxia.imagehosting.service; 2 | 3 | import java.io.File; 4 | import java.util.HashMap; 5 | import java.util.Map; 6 | 7 | import org.apache.http.HttpEntity; 8 | import org.apache.http.entity.mime.MultipartEntityBuilder; 9 | 10 | import com.alibaba.fastjson.JSON; 11 | 12 | /** 13 | * @author kalman03 14 | * @since 2019-08-17 15 | */ 16 | public class JueJinImageUploadService extends AbstractInnerImageUploadService { 17 | 18 | @Override 19 | String getUploadUrl() { 20 | return "https://cdn-ms.juejin.im/v1/upload?bucket=gold-user-assets"; 21 | } 22 | 23 | @Override 24 | HttpEntity getHttpEntity(File imageFile) { 25 | return MultipartEntityBuilder.create().addBinaryBody("file", imageFile).build(); 26 | } 27 | 28 | @Override 29 | String anaysisResult(String response) { 30 | try { 31 | System.out.println(response); 32 | return JSON.parseObject(response).getJSONObject("d").getJSONObject("url").getString("https"); 33 | } catch (Exception e) { 34 | // ignore 35 | } 36 | return null; 37 | } 38 | 39 | @Override 40 | Map getHeaderMap() { 41 | return new HashMap(); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/main/java/com/bushangbuxia/imagehosting/service/NeteaseImageUploadService.java: -------------------------------------------------------------------------------- 1 | package com.bushangbuxia.imagehosting.service; 2 | 3 | import java.io.File; 4 | import java.util.HashMap; 5 | import java.util.Map; 6 | 7 | import org.apache.http.HttpEntity; 8 | import org.apache.http.entity.mime.MultipartEntityBuilder; 9 | 10 | import com.alibaba.fastjson.JSON; 11 | 12 | /** 13 | * @author kalman03 14 | * @since 2019-08-17 15 | */ 16 | public class NeteaseImageUploadService extends AbstractInnerImageUploadService { 17 | 18 | @Override 19 | String getUploadUrl() { 20 | return "http://you.163.com/xhr/file/upload.json"; 21 | } 22 | 23 | @Override 24 | HttpEntity getHttpEntity(File imageFile) { 25 | return MultipartEntityBuilder.create().addBinaryBody("file", imageFile).build(); 26 | } 27 | 28 | @Override 29 | String anaysisResult(String response) { 30 | try { 31 | return JSON.parseObject(response).getString("data"); 32 | } catch (Exception e) { 33 | //ignore 34 | } 35 | return null; 36 | } 37 | 38 | @Override 39 | Map getHeaderMap() { 40 | return new HashMap(); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/main/java/com/bushangbuxia/imagehosting/service/PrntscrImageUploadService.java: -------------------------------------------------------------------------------- 1 | package com.bushangbuxia.imagehosting.service; 2 | 3 | import java.io.File; 4 | import java.util.HashMap; 5 | import java.util.Map; 6 | 7 | import org.apache.http.HttpEntity; 8 | import org.apache.http.entity.mime.MultipartEntityBuilder; 9 | 10 | import com.alibaba.fastjson.JSON; 11 | 12 | /** 13 | * @author kalman03 14 | * @since 2019-08-17 15 | */ 16 | public class PrntscrImageUploadService extends AbstractInnerImageUploadService { 17 | 18 | @Override 19 | String getUploadUrl() { 20 | return "https://prntscr.com/upload.php"; 21 | } 22 | 23 | @Override 24 | HttpEntity getHttpEntity(File imageFile) { 25 | return MultipartEntityBuilder.create().addBinaryBody("image", imageFile).build(); 26 | } 27 | 28 | @Override 29 | String anaysisResult(String response) { 30 | System.out.println(response); 31 | try { 32 | return JSON.parseObject(response).getString("data"); 33 | } catch (Exception e) { 34 | // ignore 35 | } 36 | return null; 37 | } 38 | 39 | @Override 40 | Map getHeaderMap() { 41 | Map map = new HashMap(); 42 | map.put("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:65.0) Gecko/20100101 Firefox/65.0"); 43 | return map; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/main/java/com/bushangbuxia/imagehosting/service/SouhuImageUploadService.java: -------------------------------------------------------------------------------- 1 | package com.bushangbuxia.imagehosting.service; 2 | 3 | import java.io.File; 4 | import java.util.HashMap; 5 | import java.util.Map; 6 | 7 | import org.apache.http.HttpEntity; 8 | import org.apache.http.entity.mime.MultipartEntityBuilder; 9 | 10 | import com.alibaba.fastjson.JSON; 11 | 12 | /** 13 | * @author kalman03 14 | * @since 2019-08-17 15 | */ 16 | public class SouhuImageUploadService extends AbstractInnerImageUploadService { 17 | 18 | @Override 19 | String getUploadUrl() { 20 | return "http://changyan.sohu.com/api/2/comment/attachment"; 21 | } 22 | 23 | @Override 24 | HttpEntity getHttpEntity(File imageFile) { 25 | return MultipartEntityBuilder.create().addBinaryBody("file", imageFile).build(); 26 | } 27 | 28 | @Override 29 | String anaysisResult(String response) { 30 | try { 31 | response = response.replaceAll("\\\\\"", "\""); 32 | response = response.substring(1,response.length()-1); 33 | return JSON.parseObject(response).getString("url"); 34 | } catch (Exception e) { 35 | // ignore 36 | } 37 | return null; 38 | } 39 | 40 | @Override 41 | Map getHeaderMap() { 42 | return new HashMap(); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/main/java/com/bushangbuxia/imagehosting/service/SuNingImageUploadService.java: -------------------------------------------------------------------------------- 1 | package com.bushangbuxia.imagehosting.service; 2 | 3 | import java.io.File; 4 | import java.util.HashMap; 5 | import java.util.Map; 6 | 7 | import org.apache.http.HttpEntity; 8 | import org.apache.http.entity.ContentType; 9 | import org.apache.http.entity.mime.MultipartEntityBuilder; 10 | import org.apache.http.entity.mime.content.StringBody; 11 | 12 | import com.alibaba.fastjson.JSON; 13 | import com.alibaba.fastjson.JSONObject; 14 | 15 | /** 16 | * @author kalman03 17 | * @since 2019-08-17 18 | */ 19 | public class SuNingImageUploadService extends AbstractInnerImageUploadService { 20 | 21 | @Override 22 | String getUploadUrl() { 23 | return "http://review.suning.com/imageload/uploadImg.do"; 24 | } 25 | 26 | @Override 27 | HttpEntity getHttpEntity(File imageFile) { 28 | return MultipartEntityBuilder.create().addBinaryBody("Filedata", imageFile) 29 | .addPart("omsOrderItemId", new StringBody("1", ContentType.DEFAULT_TEXT)) 30 | .addPart("custNum", new StringBody("1", ContentType.DEFAULT_TEXT)) 31 | .addPart("deviceType", new StringBody("1", ContentType.DEFAULT_TEXT)).build(); 32 | } 33 | 34 | @Override 35 | String anaysisResult(String response) { 36 | try { 37 | JSONObject jsonObject = JSON.parseObject(response); 38 | String src = jsonObject.getString("src"); 39 | if (src == null || src.isEmpty()) { 40 | return null; 41 | } 42 | return "https:" + src + ".jpg"; 43 | } catch (Exception e) { 44 | } 45 | return null; 46 | } 47 | 48 | @Override 49 | Map getHeaderMap() { 50 | return new HashMap(); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src/main/java/com/bushangbuxia/imagehosting/service/VimCNImageService.java: -------------------------------------------------------------------------------- 1 | package com.bushangbuxia.imagehosting.service; 2 | 3 | import java.io.File; 4 | import java.util.HashMap; 5 | import java.util.Map; 6 | 7 | import org.apache.http.HttpEntity; 8 | import org.apache.http.entity.mime.MultipartEntityBuilder; 9 | 10 | /** 11 | * @author kalman03 12 | * @since 2019-08-17 13 | */ 14 | public class VimCNImageService extends AbstractInnerImageUploadService { 15 | 16 | @Override 17 | String getUploadUrl() { 18 | return "https://img.vim-cn.com"; 19 | } 20 | 21 | @Override 22 | HttpEntity getHttpEntity(File imageFile) { 23 | return MultipartEntityBuilder.create().addBinaryBody("file", imageFile) 24 | .build(); 25 | } 26 | 27 | @Override 28 | String anaysisResult(String response) { 29 | try { 30 | System.out.println(response); 31 | return response.trim(); 32 | } catch (Exception e) { 33 | // ignore 34 | } 35 | return null; 36 | } 37 | 38 | @Override 39 | Map getHeaderMap() { 40 | Map map = new HashMap<>(); 41 | return map; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/main/java/com/bushangbuxia/imagehosting/utils/RandomUtils.java: -------------------------------------------------------------------------------- 1 | package com.bushangbuxia.imagehosting.utils; 2 | 3 | import java.util.Random; 4 | 5 | public class RandomUtils{ 6 | 7 | private static char[] numbersAndLetters = ("abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ").toCharArray(); 8 | 9 | public static final String randomString(int length) { 10 | if (length < 1) { 11 | return null; 12 | } 13 | char[] randBuffer = new char[length]; 14 | for (int i = 0; i < randBuffer.length; i++) { 15 | randBuffer[i] = numbersAndLetters[new Random().nextInt(61)]; 16 | } 17 | return new String(randBuffer); 18 | } 19 | 20 | public static final int randomNumber(int min, int max) { 21 | return new Random().ints(min, (max + 1)).limit(1).findFirst().getAsInt(); 22 | } 23 | } --------------------------------------------------------------------------------