最近作項目,須要上傳文件,由於上傳到項目路徑下,感受有時候也挺不方便的,就試了一下上傳文件到阿里雲oss上去了,java
oss的使用網上有不少介紹,都是去配置一下須要的數據,而後直接調用他的api就能夠了。數據庫
這裏貼一段能夠直接使用的oss代碼,有須要的能夠本身參考下。api
@Controller @RequestMapping("/ossfile") public class OSSFileController { @Autowired private EventidService eventidService; //----------------------------------------------------------------------------------------------- private final static String OSS_PATH = ""; /** * 阿里雲ACCESS_ID */ private static String ACCESS_ID = ""; /** * 阿里雲ACCESS_KEY */ private static String ACCESS_KEY = ""; /** * 阿里雲OSS_ENDPOINT 青島Url */ private static String OSS_ENDPOINT = ""; /** * 阿里雲BUCKET_NAME OSS */ private static String BUCKET_NAME = ""; @RequestMapping("/uploadfile") public String uploadfile(@RequestParam("file") MultipartFile file, @RequestParam("eventId") int eventId, HttpSession session) throws FileNotFoundException { ErrorResult result = new ErrorResult(); //先提早設置下oss的部分 OSSClient client = new OSSClient(OSS_ENDPOINT, ACCESS_ID, ACCESS_KEY); OSSNewUtils.ensureBucket(client, BUCKET_NAME); OSSNewUtils.setBucketPublicReadable(client, BUCKET_NAME); String Objectkey = file.getOriginalFilename(); String filename = file.getOriginalFilename(); System.out.println("文件名字"+file.getName()); //這裏給提交過來的文件設置一個本地路徑, // 而後提交過來的文件先存入該路徑, // 而後再從該路徑提交到阿里雲服務器 // 獲得uploadFile的絕對路徑 String realPath = session.getServletContext().getRealPath("uploadFile"); // 將文件放在這個路徑下 File filedir = new File(realPath,file.getOriginalFilename()); // 建立uploadFile目錄 filedir.getParentFile().mkdir(); /*這裏要寫提交的文件地址,也就是已經下載到本地的本地文件地址*/ String uploadFilePath = filedir.toString(); //建立本地文件夾後,先將文件存入本地文件夾 try { // 將上傳的文件寫入到本地中 file.transferTo(filedir); session.setAttribute("imgPath", file.getOriginalFilename()); System.out.println("文件上傳到本地文件夾完成,本地文件夾地址:"+ filedir.toString()/*uploadFilePath*/); } catch (Exception e) { e.printStackTrace(); System.err.println("文件上傳到本地文件夾失敗\n"); session.setAttribute("errMsg", e.toString()); } System.out.println("開始上傳到阿里雲服務器."); System.out.println("正在上傳..."); OSSNewUtils.uploadFile(client, BUCKET_NAME, Objectkey, uploadFilePath); System.out.println("上傳文件到阿里雲成功"); //從阿里雲服務器下載文件的存儲地址 //String downloadFilePath = "C:\\ccc\\"; //System.out.println("正在下載..."); //downloadFile(client, BUCKET_NAME, Objectkey, downloadFilePath); //發送文件請求,也須要攜帶你的eventid事件的id,才能存取地址到相應的eventid中去 Eventid eventid = new Eventid(); eventid = eventidService.selectByPrimaryKey(eventId); //設置數據庫地址 eventid.setEventMediafile(OSS_PATH+"/"+Objectkey); //將阿里雲上的地址保存到數據庫中去 int resul = eventidService.updateByPrimaryKey(eventid); System.out.println("更新數據庫文件地址字段成功!"); result.setStatus(200); result.setError("成功的上傳文件到阿里雲"); result.setData(file); return "uploadsuccess"; } }
還有一段使用到的oss工具類封裝好了,直接使用吧。服務器
public class OSSNewUtils { /** * 建立Bucket * * @param client OSSClient對象 * @param bucketName BUCKET名 * @throws OSSException * @throws ClientException */ public static void ensureBucket(OSSClient client, String bucketName)throws OSSException, ClientException { try{ client.createBucket(bucketName); }catch(ServiceException e){ if(!OSSErrorCode.BUCKET_ALREADY_EXISTS.equals(e.getErrorCode())){ throw e; } } } /** * 刪除一個Bucket和其中的Objects * * @param client OSSClient對象 * @param bucketName Bucket名 * @throws OSSException * @throws ClientException */ public static void deleteBucket(OSSClient client, String bucketName)throws OSSException, ClientException{ ObjectListing ObjectListing = client.listObjects(bucketName); List<OSSObjectSummary> listDeletes = ObjectListing.getObjectSummaries(); for(int i = 0; i < listDeletes.size(); i++){ String objectName = listDeletes.get(i).getKey(); System.out.println("objectName = " + objectName); //若是不爲空,先刪除bucket下的文件 client.deleteObject(bucketName, objectName); } client.deleteBucket(bucketName); } /** * 把Bucket設置成全部人可讀 * * @param client OSSClient對象 * @param bucketName Bucket名 * @throws OSSException * @throws ClientException */ public static void setBucketPublicReadable(OSSClient client, String bucketName)throws OSSException, ClientException{ //建立bucket client.createBucket(bucketName); //設置bucket的訪問權限, public-read-write權限 client.setBucketAcl(bucketName, CannedAccessControlList.PublicRead); } /** * 上傳文件 * * @param client OSSClient對象 * @param bucketName Bucket名 * @param Objectkey 上傳到OSS起的名 * @param filename 本地文件名 * @throws OSSException * @throws ClientException * @throws FileNotFoundException */ public static RespInfo uploadFile(OSSClient client, String bucketName, String Objectkey, String filename) throws OSSException, ClientException, FileNotFoundException { RespInfo respInfo = new RespInfo(); File file = new File(filename); ObjectMetadata objectMeta = new ObjectMetadata(); objectMeta.setContentLength(file.length()); //判斷上傳類型,多的可根據本身需求來斷定 if (filename.endsWith("xml")) { objectMeta.setContentType("text/xml"); } else if (filename.endsWith("jpg")) { objectMeta.setContentType("image/jpeg"); } else if (filename.endsWith("png")) { objectMeta.setContentType("image/png"); } InputStream input = new FileInputStream(file); client.putObject(bucketName, Objectkey, input, objectMeta); respInfo.setContent(filename); return respInfo; } /** * 下載文件 * * @param client OSSClient對象 * @param bucketName Bucket名 * @param Objectkey 上傳到OSS起的名 * @param filename 文件下載到本地保存的路徑 * @throws OSSException * @throws ClientException */ public static void downloadFile(OSSClient client, String bucketName, String Objectkey, String filename) throws OSSException, ClientException { client.getObject(new GetObjectRequest(bucketName, Objectkey), new File(filename)); } }
oss還有不少功能能夠使用,過幾天本身再試用一下其餘的功能。session