天天看點

oss的上傳和下載下傳(1)

首先當然是開通 oos 服務啦之後建立一個backet 取名haha174

順帶建立幾個分類檔案夾如下圖

oss的上傳和下載下傳(1)

接下來依次去到上傳所需要的資料建立一個工具類來儲存友善使用

pom 依賴

<dependency>
      <groupId>com.aliyun.oss</groupId>
      <artifactId>aliyun-sdk-oss</artifactId>
      <version>2.7.0</version>
    </dependency>           
public class FilePath {
    public static final String RESOURCE_PATH="D:/";
    public static final String URL_PATH="www.haha174.top:8086/dbz/";
    //  oos   相關   資料
    public static final String endpoint="http://oss-cn-beijing.aliyuncs.com";
    public static final  String accessKeyId = "LTAIWOJiEXDii";
    public static final  String accessKeySecret = "0GQxrDVLr1ohyCMOuZpUvg";
    public static final String bucketName="haha174";
    public static final String accessUrl="http://haha174.oss-cn-beijing.aliyuncs.com";
    //  oos   相關   結束
    // 下載下傳檔案零時存放  和  pdf  臨時存放路徑
    public static final String PDF_PATH="D://";
}           

當然了上述的asskey 和accessKeySecret 是假的 可以去https://ak-console.aliyun.com/?spm=5176.8465980.home.dindex1.7fcf7b345iZLRa#/accesskey 檢視自己的id 和accessKeySecret

接下來寫一個工具類做上傳用

public static String uploadFile(MultipartFile multipartFile, String remotePath) throws Exception {
            // 流轉換 将MultipartFile轉換為oss所需的InputStream  
           // CommonsMultipartFile cf = (CommonsMultipartFile) multipartFile;  
           // DiskFileItem fi = (DiskFileItem) cf.getFileItem();  
            InputStream fileContent = multipartFile.getInputStream();  
            //擷取檔案名,對檔案名做随機處理  
            String fileName = multipartFile.getOriginalFilename();  
            System.out.println(fileName);
            //這裡可以修改檔案的命名
            fileName = "dbz_" + new Date().getTime() + fileName.substring(fileName.lastIndexOf("."));
            // 加載配置檔案,初始化OSSClient  
          //  OSSConfigure ossConfigure = new OSSConfigure("/system.properties");
            OSSClient ossClient = new OSSClient(FilePath.endpoint, FilePath.accessKeyId,
                    FilePath.accessKeySecret);
            // 定義二級目錄  
            String remoteFilePath = remotePath.substring(, remotePath.length()).replaceAll("\\\\", "/") + "/";  
            // 建立上傳Object的Metadata  
            ObjectMetadata objectMetadata = new ObjectMetadata();  
            objectMetadata.setContentLength(fileContent.available());  
            objectMetadata.setContentEncoding("utf-8");  
            objectMetadata.setCacheControl("no-cache");  
            objectMetadata.setHeader("Pragma", "no-cache");  
            objectMetadata.setContentType(contentType(fileName.substring(fileName.lastIndexOf("."))));  
            objectMetadata.setContentDisposition("inline;filename=" + fileName);  
            // 上傳檔案
            System.out.print(remoteFilePath);
            ossClient.putObject(FilePath.bucketName, remoteFilePath+fileName , fileContent, objectMetadata);
            // 關閉OSSClient  
            ossClient.shutdown();  
            // 關閉io流  
            fileContent.close();  
            return FilePath.accessUrl + "/" + remoteFilePath + fileName;
        }           

之後通過controller 調用

@RequestMapping("/oos.do")
    public String UploadVideo(MultipartFile file,HttpSession sesssion) throws ParseException {

        boolean flag = true;
        RandomUtil util = new RandomUtil();
        if (file != null) {
                try {String type = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".") + );
                    //String filePath = Resource + util.getTimeString() + util.getRandom(4) + "." + type;

                    // 轉存檔案
                    //file.transferTo(new File(FilePath.RESOURCE_PATH+filePath));
                    //下面的other  是你bucket中德分類檔案夾名稱
                    String filePath=   new OSSManageUtil().uploadFile(file,"other");
                    return  filePath;
                    //得到新的檔案名
                   // list.add(new Msg(size, FilePath.URL_PATH+filePath));
                } catch (Exception e) {
                    e.printStackTrace();
                    return  "上傳失敗";
                }

        } else {
        }
        return  "上傳成功";
    }           

建立一個jsp 頁面來做檔案上傳

<form  action="/upload/oos.do" method="post" id="ImageForm" enctype="multipart/form-data" >
<input id='upinput' type='file' name='file'>
<input type='submit' value="上傳">
</form>           

啟動項目上傳圖檔

oss的上傳和下載下傳(1)

選擇一張圖檔上傳成功的話會傳回 通路位址如下圖

oss的上傳和下載下傳(1)

簡單的 java sdk 就上傳成功了

首先當然是開通 oos 服務啦之後建立一個backet 取名haha174

順帶建立幾個分類檔案夾如下圖

oss的上傳和下載下傳(1)

接下來依次去到上傳所需要的資料建立一個工具類來儲存友善使用

pom 依賴

oss