1. 程式人生 > >java後臺開發免費的物件儲存Bmob

java後臺開發免費的物件儲存Bmob

對於很多開發者來說,特別是學生黨來說,找一個不要錢的物件儲存再好不過了,下面給大家推薦bmob給大家,官方網站https://www.bmob.cn/,他提供儲存少量是免費的,對於開發者來說足夠了
一、首先是匯入

<dependencies>
    <dependency>
        <groupId>com.github.bmob</groupId>
        <artifactId>javasdk</artifactId>
        <version>1.0.1-rc1</version>
    </dependency>
    <dependency>
        <groupId>com.squareup.retrofit2</groupId>
        <artifactId>retrofit</artifactId>
        <version>2.5.0</version>
    </dependency>
    <dependency>
        <groupId>com.squareup.retrofit2</groupId>
        <artifactId>converter-gson</artifactId>
        <version>2.5.0</version>
    </dependency>
    <dependency>
        <groupId>com.squareup.retrofit2</groupId>
        <artifactId>adapter-rxjava</artifactId>
        <version>2.5.0</version>
    </dependency>
    <dependency>
        <groupId>com.squareup.okhttp3</groupId>
        <artifactId>logging-interceptor</artifactId>
        <version>3.12.0</version>
    </dependency>
    <dependency>
        <groupId>commons-fileupload</groupId>
        <artifactId>commons-fileupload</artifactId>
        <version>1.3.1</version>
    </dependency>
</dependencies>

二、後臺程式碼

 @RequestMapping(value = "/doUpload", method = RequestMethod.POST)
    public void doUploadFile(@RequestParam("file") MultipartFile file) {
        File f = null;
        if (file.equals("") || file.getSize() <= 0) {
            file = null;
        } else {
            InputStream ins = null;
            try {
                ins = file.getInputStream();
            } catch (IOException e) {
                e.printStackTrace();
            }
            f = new File(file.getOriginalFilename());
            inputStreamToFile(ins, f);
        }
        uploadFile(f);

    }

    /**
     * MultipartFile轉file
     * @param ins InputStream
     * @param file File
     */
    private 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();
        }
    }

    /**
     * 上傳圖片返回URL
     * @param file File
     */
    private static void uploadFile(File file) {

        Bmob.getInstance().init("id", "key");//bmob上申請的
        final BmobFile bmobFile = new BmobFile(file);
        bmobFile.uploadFile(new UploadListener() {
            @Override
            public void onSuccess() {
                System.out.println(bmobFile.getCdnName() + "-" + bmobFile.getFilename() + "-" + bmobFile.getUrl());
            }
            @Override
            public void onFailure(BmobException ex) {
                System.err.println(ex.getMessage());
            }
        });
    }

三,前端程式碼

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>$Title$</title>
  </head>
  <body>

  <form method="post" action="/doUpload" enctype="multipart/form-data">
    <input type="file" name="file"/>
    <button type="submit" >提交</button>
  </form>

  </body>
</html>

四、總結
這樣就完成了圖片上傳,然後bmob那邊返回圖片URL,開發者就可以對URL做出相應的處理了,是不是很簡單啊