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