Write in front
There are not a few articles about qiniu cloud uploading pictures, but they just use the test domain name given by qiniu cloud. As a webmaster, after upgrading his domain name to https, the obsessive-compulsive patients are not allowed to have the three words of insecurity in the browser. Of course, there will be some holes when using qiniu cloud to bind his domain name. In order to better maintain their own website, not necessarily perfect for all, but try to do the best!
preparation
- Because my personal server belongs to alicloud, I applied for a secondary domain name on my alicloud (how to apply for alicloud secondary domain name: Click here)
- After the second level domain name application is completed, you need to purchase an ssl certificate. Alicloud has a free certificate to apply for another one!
- And download the ssl certificate! (no configuration such as Nginx is required.)
Seven cattle cloud configuration
New storage
The space name can be customized, and access control selection is public.
At this point, you are finished with the new one.
Configure domain name
Upload https certificate, first level domain name certificate and second level domain name certificate to qiniu cloud first.
In space management, click domain name to configure domain name. First bind primary domain name, then bind secondary domain name. The binding is completed as follows.
Of course, there are pits here. If cname is not used, the following problems will occur.
If you directly create A new cname in the primary domain name, that is, in Alibaba cloud, the record value and - A will always conflict. If you modify - A directly, the following problems will occur.
So we need a secondary domain name!
code implementation
pom introduces jar package
<!--Seven cattle server--> <dependency> <groupId>com.qiniu</groupId> <artifactId>qiniu-java-sdk</artifactId> <version>7.2.11</version> <scope>compile</scope> </dependency> <dependency> <groupId>com.squareup.okhttp3</groupId> <artifactId>okhttp</artifactId> <version>3.3.1</version> <scope>compile</scope> </dependency> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.6.2</version> <scope>compile</scope> </dependency> <dependency> <groupId>com.qiniu</groupId> <artifactId>happy-dns-java</artifactId> <version>0.1.4</version> <scope>compile</scope> </dependency> <dependency> <groupId>com.squareup.okhttp3</groupId> <artifactId>okhttp</artifactId> <version>3.3.1</version> <scope>compile</scope> </dependency> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.6.2</version> <scope>compile</scope> </dependency> <!-- Request header parameter analysis package --> <dependency> <groupId>eu.bitwalker</groupId> <artifactId>UserAgentUtils</artifactId> <version>1.20</version> </dependency> <!--Qiniu cloud upload picture service--> <dependency> <groupId>com.qiniu</groupId> <artifactId>sdk</artifactId> <version>6.1.0</version> </dependency>
Configure SDK
This sdk is under the management of the secret key of qiniu cloud
/** * Class action description: named class * Creation time: December 1, 2018 17:54 * Construction method parameters: * Revised on: December 1, 2018 17:54 * Created by: ZENG * Class fully qualified name: com.hyxiaojingyu.common.VariableName **/ public class QiNiuSdk { // Seven cattle AK public static final String accessKey = "ak"; // Seven cattle SK public static final String secretKey = "sk"; // Seven cattle storage space name public static final String bucket = "Space name"; // Seven cattle default domain name public static final String domain = "Secondary domain name"; }
Code implementation
/** * Class function description: upload image to server * Creation time: December 1, 2018 17:54 * Construction method parameters: * Revised on: December 1, 2018 17:54 * Created by: ZENG * Class fully qualified name: com.hyxiaojingyu.common.QiniuUpload **/ public class QiniuUpload { //Set the access of account_ Key and SECRET_KEY private static String ACCESS_KEY = QiNiuSdk.accessKey; //These two accounts can be found in the account of qiniu private static String SECRET_KEY = QiNiuSdk.secretKey; //Space to upload private static String bucketname = QiNiuSdk.bucket; //Corresponding to the path you want to upload to qiniu (pay attention to public settings when creating your own folder) //Key configuration private static Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY); private static Configuration cfg = new Configuration(Zone.huanan()); //Create upload object private static UploadManager uploadManager = new UploadManager(cfg); //Simple upload, use the default policy, just set the upload space name public static String getUpToken(){ return auth.uploadToken(bucketname); } public static String UploadPic(String FilePath,String FileName){ Configuration cfg = new Configuration(Zone.huanan()); UploadManager uploadManager = new UploadManager(cfg); String accessKey = QiNiuSdk.accessKey; //AccessKey value String secretKey = QiNiuSdk.secretKey; //The value of the secret key String bucket = QiNiuSdk.bucket; //Storage space name Auth auth = Auth.create(accessKey, secretKey); String upToken = auth.uploadToken(bucket); try { Response response = uploadManager.put(FilePath, FileName, upToken); //Analyze the result of upload success DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class); System.out.println(putRet.key); System.out.println(putRet.hash); return QiNiuSdk.domain+FileName; }catch (QiniuException ex){ Response r = ex.response; System.err.println(r.toString()); try { System.err.println(r.bodyString()); } catch (QiniuException ex2) { //ignore } } return null; } public static String updateFile(MultipartFile file, String filename) throws Exception { //If no key is specified by default, the hash value of the file content is used as the file name try { InputStream inputStream=file.getInputStream(); ByteArrayOutputStream swapStream = new ByteArrayOutputStream(); byte[] buff = new byte[600]; //buff is used to store the temporary data read by the loop int rc = 0; while ((rc = inputStream.read(buff, 0, 100)) > 0) { swapStream.write(buff, 0, rc); } byte[] uploadBytes = swapStream.toByteArray(); try { com.qiniu.http.Response response = uploadManager.put(uploadBytes,filename,getUpToken()); //Analyze the result of upload success DefaultPutRet putRet; putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class); return QiNiuSdk.domain+putRet.key; } catch (QiniuException ex) { Response r = ex.response; System.err.println(r.toString()); try { System.err.println(r.bodyString()); } catch (QiniuException ex2) { } } } catch (UnsupportedEncodingException ex) { } return null; } }
@ResponseBody @PostMapping("/upload") public HashMap<String, Object> uploadImageByCover(MultipartFile file){ HashMap<String,Object> result = new HashMap<>(); try { // Get file name String fileName = file.getOriginalFilename(); //Results March 1, 2017 String format = DateUtil.format(new Date(), "yyyy-MM-dd-HH-mm-ss"); String newFilename = format+"-"+fileName; String s = QiniuUpload.updateFile(file, newFilename); result.put("msg",s); } catch (Exception e) { e.printStackTrace(); } return result; }
test