SPRINGMVC-File upload-use Ali oss file

Source: Internet
Author: User

The company used Ali's file server OSS, we used to SPRINGMVC upload files.

First add Ali's jar to my MAVEN project.

Copy these Java classes from the following sample. ,

public class ossobjectsample { private static final string access_id  =  * * * * *; private static final string access_key =  * * * *;  private static final string bucket_name =  "111"; private static  Ossclient client = null; private static void initialize ()  {   if  (client == null)  {   client = new ossclient (ACCESS_ Id, access_key);  } } /**  *   *  upload file  2015 July 2 14 :11:27  *   *  @param  key  *              Server file name   *   *  @param  file   *             files to upload   *    *  @param  servicename  *             the path that the server wants to access   *  @return   *  @throws  OSSException  *  @throws   clientexception  *  @throws  filenotfoundexception  */ public static  string ossputobject (String key, file file, string servicename)  {   initialize ();   objectmetadata objectmeta = new objectmetadata ();   objectmeta.setcontentlength (File.length ());  //  can mark file types in metadata//   Objectmeta.setcontenttype ("Image/jpeg");  inputstream input;  try {    input = new fileinputstream (file);    client.putobject (BUCKET_NAME,  servicename +  "/"  + key, input,     objectmeta);   } catch  (FILENOTFOUNDEXCEPTION&NBsp;e)  {   // TODO Auto-generated catch block    E.printstacktrace ();  }  return  "http://"  + BUCKET_NAME +  ". oss-cn-hangzhou.aliyuncs.com/"    + serviceName + "/" + key;  }  public static string ossputobject (string key,multipartfile  Multipartfile , string servicename)  {  initialize ();     commonsmultipartfile commonsmultipartfile=  (Commonsmultipartfile) multipartfile;          DiskFileItem diskFileItem =  (Diskfileitem) Commonsmultipartfile.getfileitem ();          file file =  diskfileitem.getstorelocation ();         return ossputobject (key, file, servicename);  } }

Start by using the client's singleton.

Then SPRINGMVC use multipartfile This file object to receive the open source to see and file is not related to the interface is defined by their own and then continue to see if they have anything in common not found what then Baidu look I found the object commonsmultipartfile and then slowly turned around to test the success.

SPRINGMVC-File upload-use Ali oss file

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.