|
@@ -0,0 +1,223 @@
|
|
|
+package com.miaxis.common.aliyun;
|
|
|
+
|
|
|
+import com.aliyun.oss.OSSClient;
|
|
|
+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;
|
|
|
+ }
|
|
|
+}
|