七牛云图片上传

maven工程写的小demo

需要注意的你的区别选错了

七牛云图片上传_第1张图片

如果用错了的话,一般给的提示就是

{ResponseInfo:fo:com.qiniu.http.Response@5b7@5b792f7e,status:400, reqId:2Q0AADhOEv7T8F4V, xlog:UP:4/400, xvia:, adress:ss:up-z2.qiniu.com/183.236.51.11:80, dur duration:0.000000 s, error:incorrect region, please use se up-z1.qiniup.com}
{"}
{"error":"incorrect region, please use se up-z1.qiniup.com"}"}

请看please use se up-z1.qiniup.com,请用z1,这里是因为你的存储空间在华北区,所以自己选一下,对应到代码的话,在这个位置,给zone2改成zone1

// 构造一个带指定Zone对象的配置类
Configuration cfg = new Configuration(Zone.zone2());

pom.xml


	4.0.0
	com.qiniuyun
	demo
	0.0.1-SNAPSHOT

	
	
		4.2.4.RELEASE
	

	
		
			com.qiniu
			qiniu-java-sdk
			7.2.11
			compile
		
		
			com.squareup.okhttp3
			okhttp
			3.3.1
			compile
		
		
			com.google.code.gson
			gson
			2.6.2
			compile
		
		
			com.qiniu
			happy-dns-java
			0.1.4
			compile
		
		
			junit
			junit
			4.12
			test
		

		
			commons-fileupload
			commons-fileupload
			1.3.1
		

		
		
			org.springframework
			spring-context
			${spring.version}
		
		
			org.springframework
			spring-beans
			${spring.version}
		
		
			org.springframework
			spring-webmvc
			${spring.version}
		
		
			org.springframework
			spring-jdbc
			${spring.version}
		
		
			org.springframework
			spring-aspects
			${spring.version}
		
		
			org.springframework
			spring-jms
			${spring.version}
		
		
			org.springframework
			spring-context-support
			${spring.version}
		
		
			org.springframework
			spring-test
			${spring.version}
		


	



PictureUtil.java

package com.qiniuyun.utils;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

import org.springframework.web.multipart.MultipartFile;

import com.google.gson.Gson;
import com.qiniu.common.QiniuException;
import com.qiniu.common.Zone;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;

public class PictureUtil {

	public static String accessKey = "你自己的AK";
	public static String secretKey = "你自己的SK";
	public static String bucket = "你自己所用的存储命的存储空间名";

	private static String QINIU_IMAGE_DOMAIN = "http://tasssss.bkt.clouddn.com/";//改成他给你的免费的域名,或者自己解析一个域名,买一个很便宜

	//普通本地上传
	public static String upload(String localFilePath) {
		// 构造一个带指定Zone对象的配置类
		Configuration cfg = new Configuration(Zone.zone2());// 这里是华南,对应的2
		UploadManager uploadManager = new UploadManager(cfg);
		// 默认不指定key的情况下,以文件内容的hash值作为文件名
		String key = null;

		Auth auth = Auth.create(accessKey, secretKey);
		String upToken = auth.uploadToken(bucket);

		DefaultPutRet putRet = null;

		try {
			Response response = uploadManager.put(localFilePath, key, upToken);
			// 解析上传成功的结果
			putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
		} catch (QiniuException ex) {
			Response r = ex.response;
			System.err.println(r.toString());
			try {
				System.err.println(r.bodyString());
			} catch (QiniuException ex2) {
				// ignore
			}
		}
		return QINIU_IMAGE_DOMAIN + putRet.hash;
	}

	//文件流上传
	public static String upload(MultipartFile file) throws IOException {
		// 构造一个带指定Zone对象的配置类
		Configuration cfg = new Configuration(Zone.zone2());
		UploadManager uploadManager = new UploadManager(cfg);
		// 默认不指定key的情况下,以文件内容的hash值作为文件名
		String key = null;
		DefaultPutRet putRet = null;
		try {
			byte[] uploadBytes = file.getBytes();
			ByteArrayInputStream byteInputStream = new ByteArrayInputStream(uploadBytes);
			Auth auth = Auth.create(accessKey, secretKey);
			String upToken = auth.uploadToken(bucket);
			try {
				Response response = uploadManager.put(byteInputStream, key, upToken, null, null);
				putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
			} catch (QiniuException ex) {
				Response r = ex.response;
				System.err.println(r.toString());
				try {
					System.err.println(r.bodyString());
				} catch (QiniuException ex2) {
					// ignore
				}
			}
		} catch (UnsupportedEncodingException ex) {
			// ignore
		}
		return QINIU_IMAGE_DOMAIN + putRet.hash;
	}

	public static void main(String[] args) {
		String url = PictureUtil.upload("D:\\qiniu\\test.jpg");
		System.out.println(url);
	}

}




你可能感兴趣的:(后端)