Spring Learning Notes (21): Problems with the use of Aliyun oss JAVASDK (with: test code) __ Aliyun

Source: Internet
Author: User
Tags aliyun
2017-4-25 Update: Uploading files with OSS and encountering problems today
Problem Description: Unknown host:jalon-test.oss-cn-beijing.aliyuncs.com/; it was all good before, but today it was a mistake. Reason: Check the website for a long time, it is HTTPS's sake Because the OSS request header in the Beijing area has been updated from HTTP to HTTPS, causing access to the host error; Workaround: Change the ossclient endpoint configuration from http://oss-cn-beijing.aliyuncs.com to HTTPS ://oss-cn-beijing.aliyuncs.com

Previous questions:

Excerpt from: Aliyun official website https://help.aliyun.com/document_detail/32024.html
At that time, the following error occurred:

 Exception in thread "main" java.lang.noclassdeffounderror:org/apache/http/ssl/ Truststrategy at Com.aliyun.oss.ossclient.<init> (ossclient.java:268) at com.aliyun.oss.ossclient.< ;init> (ossclient.java:193) at Com.aliyun.oss.demo.HelloOSS.main (hellooss.java:77) caused By:java.lang.Clas
        SNotFoundException:org.apache.http.ssl.TrustStrategy at Java.net.urlclassloader$1.run (urlclassloader.java:366) At Java.net.urlclassloader$1.run (urlclassloader.java:355) at java.security.AccessController.doPrivileged (Nat Ive to Java.net.URLClassLoader.findClass (urlclassloader.java:354) at Java.lang.ClassLoader.loadCla SS (classloader.java:425) at Sun.misc.launcher$appclassloader.loadclass (launcher.java:308) at Java.lang.Cla Ssloader.loadclass (classloader.java:358) ... 3 more 

The reason is that there is a problem with the package dependencies, Aliyun's OSS SDK references Apache HttpClient 4.4.1, and I introduced it in my project, but the version is different, and that's why it goes wrong. Can run "mvn dependency:tree" in the engineering directory, you can see the interdependence between

Here are two ways to resolve package dependencies: use a unified version. If your project is used with Apache HttpClient
4.4.1 Conflicting versions, please also use the 4.4.1 version. Remove other versions of Apache httpclient dependencies in Pom.xml. If your project uses commons-httpclient or there may be a conflict, please remove commons-httpclient. Unbind a dependency conflict. If your project relies on multiple third-party packages, and a Third-party package relies on different versions of Apache
HttpClient, you will have a dependency conflict in your project, please use exclusion to lift. Please refer to Maven guides for details.

I used the httpclient4.3.6 in my project, the code is as follows, only the first method, the version can be raised to 4.4 1; If you use the second one to remove the httpclient that are dependent on the Aliyun SDK, it will still be wrong because the httpclient version in my project is low

<!--Apache Http client--> <dependency> <groupid>org.apache.httpcomponents</gr  
        Oupid> <artifactId>httpcore</artifactId> <version>4.3.3</version> </dependency> <dependency> <groupid>org.apache.httpcomponents</grou  
        Pid> <artifactId>httpclient</artifactId> <version>4.3.6</version> </dependency> <dependency> <groupid>org.apache.httpcomponents</groupi D> <artifactId>httpclient-cache</artifactId> &LT;VERSION&GT;4.3.6&LT;/VERSION&G  
        T  
            </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpmime</artifactId> <version>4.3.6</version> ;/dependency> <!--Apache Http client end--> 

Here is the test code:

    public static void Main (string[] args)
    {
        //endpoint take Beijing as an example, other region please fill in
        the String endpoint = "http:// Oss-cn-beijing.aliyuncs.com ";
        accesskey Please login https://ak-console.aliyun.com/#/view
        String Accesskeyid = "*****************";
        String Accesskeysecret = "********************";
        Create Ossclient instance
        ossclient client = new Ossclient (endpoint, Accesskeyid, Accesskeysecret);
        Get the root path of the project: User.dir means to get the root path of the project, not to get the User.dir directory, but on the Linux server, get the/java/apache-tomcat-8.0.24/bin/, don't know why
        String dir = system.getproperty ("User.dir");
        System.out.println (dir);
        Upload file
        client.putobject ("Jalon-test", "1.jpg", New File ("E:\\1.jpg"));
        Download file
        client.getobject (New Getobjectrequest ("Jalon-test", "1.jpg"), New file (dir+ "\\1.jpg"));
    

Here is the result: E:\SVN_WORKSPACE\SHOES_SRC, then 1.jpg has been successfully uploaded to the OSS and downloaded to the project root directory

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.