|
@@ -1,251 +0,0 @@
|
|
|
-package com.miaxis.common.aliyunOSS;
|
|
|
-
|
|
|
-import com.aliyun.oss.OSSClient;
|
|
|
-import com.miaxis.common.core.domain.Response;
|
|
|
-import com.miaxis.common.utils.Base64;
|
|
|
-import sun.misc.BASE64Decoder;
|
|
|
-
|
|
|
-import java.io.*;
|
|
|
-import java.net.URL;
|
|
|
-import java.net.URLEncoder;
|
|
|
-
|
|
|
-public class AliyunUpload {
|
|
|
-
|
|
|
- @SuppressWarnings("restriction")
|
|
|
- public static byte[] getByteFormString(String content){
|
|
|
- BASE64Decoder decoder = new BASE64Decoder();
|
|
|
- byte[] sourceBytes = null;
|
|
|
- try {
|
|
|
- sourceBytes = decoder.decodeBuffer(content);
|
|
|
- for (int i = 0; i < sourceBytes.length; ++i) {
|
|
|
- if (sourceBytes[i] < 0) {// 调整异常数据
|
|
|
- sourceBytes[i] += 256;
|
|
|
- }
|
|
|
- }
|
|
|
- } catch (Exception e) {
|
|
|
- // TODO: handle exception
|
|
|
- }
|
|
|
-
|
|
|
- return sourceBytes;
|
|
|
- }
|
|
|
- /**
|
|
|
- * 上传byte数组
|
|
|
- * @param content 比如"Hello OSS".getBytes();
|
|
|
- * @param savePath 保存的路径
|
|
|
- * @param fileName 上传文件的名称包含后坠
|
|
|
- * @return 如果上传成功返回访问地址,如果失败返回"";
|
|
|
- * */
|
|
|
- @SuppressWarnings("deprecation")
|
|
|
- public static String uploadForByte(byte[] content,String savePath,String fileName){
|
|
|
- OSSClient ossClient = new OSSClient(AliyunConfig.ENDPOINT, AliyunConfig.ACCESSKEYID,AliyunConfig.ACCESSKEYSECRET);
|
|
|
- String url = "";
|
|
|
- try {
|
|
|
- String key = savePath+fileName;
|
|
|
- ossClient.putObject(AliyunConfig.BUCKETNAME, key, new ByteArrayInputStream(content));
|
|
|
- url = AliyunConfig.VISITPATH+"/"+savePath+URLEncoder.encode(fileName);
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- return null;
|
|
|
- }finally{
|
|
|
- ossClient.shutdown();
|
|
|
- }
|
|
|
- return url;
|
|
|
- }
|
|
|
- /**
|
|
|
- * 上传本地文件
|
|
|
- * @param file 如new File("d://1.jpg")
|
|
|
- * @param savePath 保存的路径 如 D://1.jpg
|
|
|
- * @param fileName 上传文件的名称 1.jpg
|
|
|
- * @return 如果上传成功返回访问地址,如果失败返回"";
|
|
|
- * */
|
|
|
- @SuppressWarnings("deprecation")
|
|
|
- public static String uploadForLocalFile(File file, String savePath,String fileName){
|
|
|
- OSSClient ossClient = new OSSClient(AliyunConfig.ENDPOINT, AliyunConfig.ACCESSKEYID,AliyunConfig.ACCESSKEYSECRET);
|
|
|
- String url = "";
|
|
|
- try {
|
|
|
- String key = savePath+fileName;
|
|
|
- ossClient.putObject(AliyunConfig.BUCKETNAME, key, file);
|
|
|
- url = AliyunConfig.VISITPATH+"/"+savePath+URLEncoder.encode(fileName);
|
|
|
- } catch (Exception e) {
|
|
|
- //e.printStackTrace();
|
|
|
- return null;
|
|
|
- }finally{
|
|
|
- ossClient.shutdown();
|
|
|
- }
|
|
|
- return url;
|
|
|
- }
|
|
|
- /**
|
|
|
- * 上传文件流
|
|
|
- * @param is 比如InputStream inputStream = new FileInputStream("localFile");
|
|
|
- * @param savePath 保存的路径 如 D://1.jpg
|
|
|
- * @param fileName 上传文件的名称 1.jpg
|
|
|
- * @return 如果上传成功返回访问地址,如果失败返回"";
|
|
|
- * */
|
|
|
- @SuppressWarnings("deprecation")
|
|
|
- public static String uploadForStream(InputStream is, String savePath,String fileName){
|
|
|
- OSSClient ossClient = new OSSClient(AliyunConfig.ENDPOINT, AliyunConfig.ACCESSKEYID,AliyunConfig.ACCESSKEYSECRET);
|
|
|
- String url = "";
|
|
|
- try {
|
|
|
- String key = savePath+fileName;
|
|
|
- ossClient.putObject(AliyunConfig.BUCKETNAME, key, is);
|
|
|
- url = AliyunConfig.VISITPATH+"/"+savePath+URLEncoder.encode(fileName);
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- return null;
|
|
|
- }finally{
|
|
|
- ossClient.shutdown();
|
|
|
- }
|
|
|
- return url;
|
|
|
- }
|
|
|
- /**
|
|
|
- * 上传网络流
|
|
|
- * @param url 图片路径
|
|
|
- * @param savePath 保存的路径 如 D://1.jpg
|
|
|
- * @param fileName 上传文件的名称 1.jpg
|
|
|
- * @return 如果上传成功返回访问地址,如果失败返回"";
|
|
|
- * */
|
|
|
- @SuppressWarnings("deprecation")
|
|
|
- public static String uploadForUrl(String url, String savePath,String fileName){
|
|
|
- OSSClient ossClient = new OSSClient(AliyunConfig.ENDPOINT, AliyunConfig.ACCESSKEYID,AliyunConfig.ACCESSKEYSECRET);
|
|
|
- String path = "";
|
|
|
- try {
|
|
|
- String key = savePath+fileName;
|
|
|
- InputStream inputStream = new URL(url).openStream();
|
|
|
- ossClient.putObject(AliyunConfig.BUCKETNAME, key, inputStream);
|
|
|
- path = AliyunConfig.VISITPATH+"/"+savePath+URLEncoder.encode(fileName);
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- return null;
|
|
|
- }finally{
|
|
|
- ossClient.shutdown();
|
|
|
- }
|
|
|
- return path;
|
|
|
- }
|
|
|
- /**
|
|
|
- * 上传字符串
|
|
|
- * @param content 上传的内容
|
|
|
- * @param savePath 保存的路径 如 D://1.jpg
|
|
|
- * @param fileName 上传文件的名称 1.jpg
|
|
|
- * @return 如果上传成功返回访问地址,如果失败返回"";
|
|
|
- * */
|
|
|
- @SuppressWarnings("deprecation")
|
|
|
- public static String uploadForString(String content, String savePath,String fileName){
|
|
|
- OSSClient ossClient = new OSSClient(AliyunConfig.ENDPOINT, AliyunConfig.ACCESSKEYID,AliyunConfig.ACCESSKEYSECRET);
|
|
|
- String path = "";
|
|
|
- try {
|
|
|
- String key = savePath+fileName;
|
|
|
- ossClient.putObject(AliyunConfig.BUCKETNAME, key, new ByteArrayInputStream(content.getBytes()));
|
|
|
- path = AliyunConfig.VISITPATH+"/"+savePath+URLEncoder.encode(fileName);
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- return null;
|
|
|
- }finally{
|
|
|
- ossClient.shutdown();
|
|
|
- }
|
|
|
- return path;
|
|
|
- }
|
|
|
- public static void main1(String[] args) {
|
|
|
- //OSSClient ossClient = new OSSClient("http://oss-cn-beijing.aliyuncs.com", "LTAIf6iXk0nNVS6R","V8XHqsYUBlcebbN1o3FqsntYepIifB");
|
|
|
- String keyId= "LTAIijc2Vl913Dha";
|
|
|
- String accessKey="HbQBG0RQqdfKoYGJmoqdT1FFYO8OTs";
|
|
|
- OSSClient ossClient = new OSSClient("http://oss-cn-beijing.aliyuncs.com", keyId,accessKey);
|
|
|
- String path = "";
|
|
|
- try {
|
|
|
-
|
|
|
- String url = "http://218.60.2.212:8080/lnvideo/upload/weier/180228/20180228175702.mp4";
|
|
|
- try {
|
|
|
- URL temp = new URL(url);
|
|
|
- InputStream in = temp.openStream();
|
|
|
- System.out.println("连接可用");
|
|
|
- } catch (Exception e) {
|
|
|
- System.out.println("连接不可用");
|
|
|
- }
|
|
|
-
|
|
|
- String key = "video/"+url.replace("http://218.60.2.212:8080/lnvideo/", "");
|
|
|
- //String fileName = url.substring(url.lastIndexOf("/"),url.length());
|
|
|
- System.out.println(key);
|
|
|
- //System.out.println(fileName);
|
|
|
- InputStream inputStream = new URL(url).openStream();
|
|
|
- ossClient.putObject("lnfile", key, inputStream);
|
|
|
- path = AliyunConfig.VISITPATH+key;
|
|
|
- System.out.println(path);
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- }finally{
|
|
|
- ossClient.shutdown();
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- public static String GetImageStr(String imgFile)
|
|
|
- {//将图片文件转化为字节数组字符串,并对其进行Base64编码处理
|
|
|
- //String imgFile = "F:\\36.jpg";//待处理的图片
|
|
|
- InputStream in = null;
|
|
|
- byte[] data = null;
|
|
|
- //读取图片字节数组
|
|
|
- try
|
|
|
- {
|
|
|
- in = new FileInputStream(imgFile);
|
|
|
- data = new byte[in.available()];
|
|
|
- in.read(data);
|
|
|
- in.close();
|
|
|
- }
|
|
|
- catch (IOException e)
|
|
|
- {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- //对字节数组Base64编码
|
|
|
- return Base64.encode(data);//返回Base64编码过的字节数组字符串
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 上传文件流
|
|
|
- * @param is 比如InputStream inputStream = new FileInputStream("localFile");
|
|
|
- * @param savePath 保存的路径 如 D://1.jpg
|
|
|
- * @param fileName 上传文件的名称 1.jpg
|
|
|
- * @return 如果上传成功返回访问地址,如果失败返回"";
|
|
|
- * */
|
|
|
- @SuppressWarnings("deprecation")
|
|
|
- public static String uploadForGzptStream(InputStream is, String savePath,String fileName){
|
|
|
- OSSClient ossClient = new OSSClient(AliyunConfig.ENDPOINT, AliyunConfig.ACCESSKEYID,AliyunConfig.ACCESSKEYSECRET);
|
|
|
- String url = "";
|
|
|
- try {
|
|
|
- String key = savePath+fileName;
|
|
|
- ossClient.putObject(AliyunConfig.BUCKETNAME, key, is);
|
|
|
- url = savePath+URLEncoder.encode(fileName);
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- return null;
|
|
|
- }finally{
|
|
|
- ossClient.shutdown();
|
|
|
- }
|
|
|
- return url;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 删除文件
|
|
|
- * @param filePath 文件
|
|
|
- * @return
|
|
|
- */
|
|
|
- public static boolean deleteObject(String filePath) {
|
|
|
- OSSClient ossClient = new OSSClient(AliyunConfig.ENDPOINT, AliyunConfig.ACCESSKEYID,AliyunConfig.ACCESSKEYSECRET);
|
|
|
-
|
|
|
- try {
|
|
|
- boolean exist = ossClient.doesObjectExist(AliyunConfig.BUCKETNAME,filePath);
|
|
|
- if (!exist) {
|
|
|
- System.out.println("文件不存在");
|
|
|
- return false;
|
|
|
- }
|
|
|
- ossClient.deleteObject(AliyunConfig.BUCKETNAME,filePath);
|
|
|
-
|
|
|
- }catch (Exception e){
|
|
|
- e.printStackTrace();
|
|
|
- System.out.println("服务异常");
|
|
|
- return false;
|
|
|
- }finally {
|
|
|
- ossClient.shutdown();
|
|
|
- }
|
|
|
- return true;
|
|
|
- }
|
|
|
-
|
|
|
-}
|