上次朋友推荐的免费图床只有1个g,今天给大家推荐1个100g的免费图床+java后台代码上传测试
![aba9f8ef850db7e455da9d3a62ba1113.png](https://img-blog.csdnimg.cn/img_convert/aba9f8ef850db7e455da9d3a62ba1113.png)
地址为:https://images.ac.cn/
![bb90e9f3eb390e7d6ab27df35e21b074.png](https://img-blog.csdnimg.cn/img_convert/bb90e9f3eb390e7d6ab27df35e21b074.png)
看api需要什么参数
看api知道,需要这么几个参数,首先在类里面定义它
public class UploadConstant { public static final String API_TYPE = "ali"; public static final String PRIVATE_STRORAGE = "oss"; public static final String TOKEN = "填写自己的token"; public static final String URL = "https://images.ac.cn/api/upload/upload";}2.上传代码public class UploadUtil { public static JSONObject uploadImg(File image, String apiType, String privateStorage, String token, String url) { RequestBody fileBody = RequestBody.create(MediaType.parse("image/*"), image); OkHttpClient client = new OkHttpClient(); JSONObject jsonObject = null; RequestBody requestBody = new MultipartBody.Builder() .setType(MultipartBody.FORM) .addFormDataPart("image", image.getName(), fileBody) .addFormDataPart("apiType", apiType) .addFormDataPart("privateStorage", privateStorage) .addFormDataPart("token", token) .build(); System.out.println(requestBody.toString()); Request request = new Request.Builder() .url(url) .post(requestBody) .build(); Response response; try { response = client.newCall(request).execute(); String jsonString = response.body().string(); jsonObject = JSON.parseObject(jsonString); System.out.println("返回:"+jsonObject.getJSONObject("data").getJSONObject("url").getString("ali")); if(!jsonObject.getString("code").equals("200")){ System.out.println("图片上传土豆图床错误。。。。"); }else{ } } catch (IOException e) { }catch (JSONException e){ } return jsonObject; } //获取流文件 public static void inputStreamToFile(InputStream ins, File file) { try { OutputStream os = new FileOutputStream(file); int bytesRead = 0; byte[] buffer = new byte[8192]; while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) { os.write(buffer, 0, bytesRead); } os.close(); ins.close(); } catch (Exception e) { e.printStackTrace(); } }}
![2b65bec022aa117f22399c4ec62a57f3.png](https://img-blog.csdnimg.cn/img_convert/2b65bec022aa117f22399c4ec62a57f3.png)
查看自己的图床是否上传了
注意:后台上传图片,主要用到了okhttp3这个jar,他的pom配置为
com.squareup.okhttp3 okhttp 4.4.0