Java是一門面向?qū)ο缶幊陶Z言,不僅吸收了C++語言的各種優(yōu)點,還摒棄了C++里難以理解的多繼承、指針等概念,因此Java語言具有功能強大和簡單易用兩個特征。Java語言作為靜態(tài)面向?qū)ο缶幊陶Z言的代表,極好地實現(xiàn)了面向?qū)ο罄碚摚试S程序員以優(yōu)雅的思維方式進行復(fù)雜的編程 。
成都創(chuàng)新互聯(lián)公司成立與2013年,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項目成都做網(wǎng)站、網(wǎng)站建設(shè)網(wǎng)站策劃,項目實施與項目整合能力。我們以讓每一個夢想脫穎而出為使命,1280元南鄭做網(wǎng)站,已為上家服務(wù),為南鄭各地企業(yè)和個人服務(wù),聯(lián)系電話:18982081108
Java具有簡單性、面向?qū)ο?、分布式、健壯性、安全性、平臺獨立與可移植性、多線程、動態(tài)性等特點 。Java可以編寫桌面應(yīng)用程序、Web應(yīng)用程序、分布式系統(tǒng)和嵌入式系統(tǒng)應(yīng)用程序等 。
第二步驟:主要功能實現(xiàn)。springboot默認是集成springmvc,使用springboot和直接使用springmvc上傳是一樣的。springboot默認是集成springmvc,使用springboot和直接使用springmvc上傳是一樣的。
2、前端代碼:
1、具體代碼如下所示:
此處直接使用的表單同步提交。
<!DOCTYPE html> <html> <head> <title>圖片上傳</title> <meta name="keywords" content="keyword1,keyword2,keyword3"></meta> <meta name="description" content="this is my page"></meta> <meta name="content-type" content="text/html; charset=UTF-8"></meta> </head> <body> <form enctype="multipart/form-data" method="post" action="/testUploadimg"> 圖片:<input type="file" name="file" /><br/> <input type="submit" value="上傳" />. </form> </body> </html>
控制器UploadController 實現(xiàn)
UploadController 主要分為3部分
1.1 調(diào)整頁面請求goUploadImg
1.2 上傳請求方法uploadImg
1.3 存儲圖片方法uploadFile
@Controllerpublic class UploadController { //跳轉(zhuǎn)到上傳文件的頁面 @RequestMapping(value = "/gouploadimg", method = RequestMethod.GET) public String goUploadImg() { //跳轉(zhuǎn)到 templates 目錄下的 uploadimg.html return "uploadimg"; } //處理文件上傳 @ResponseBody //返回json數(shù)據(jù) @RequestMapping(value = "/testUploadimg", method = RequestMethod.POST) public String uploadImg(@RequestParam("file") MultipartFile file, HttpServletRequest request) { tring contentType = file.getContentType(); String fileName = file.getOriginalFilename(); String filePath = "D:/img"; if (file.isEmpty()) { return "文件為空!"; } try { uploadFile(file.getBytes(), filePath, fileName); } catch (Exception e) { // TODO: handle exception } //返回json return "上傳成功"; } public static void uploadFile(byte[] file, String filePath, String fileName) throws Exception { File targetFile = new File(filePath); if (!targetFile.exists()) { targetFile.mkdirs(); } FileOutputStream out = new FileOutputStream(filePath +"/"+ fileName); out.write(file); out.flush(); out.close(); } }
2:同時需要將上傳圖片的原始文件名和存儲文件名、以及關(guān)聯(lián)id存入一個數(shù)據(jù)表中。
2.1 將存儲文件名設(shè)置為UUID,避免存儲文件名重復(fù)
public static String getUUID(){ UUID uuid=UUID.randomUUID(); String str = uuid.toString(); String uuidStr=str.replace("-", ""); return uuidStr; }
2.2 將存儲文件名按照時間生成,避免存儲文件名重復(fù)
System.nanoTime()
該函數(shù)是返回納秒的。1毫秒=1納秒*1000*1000
如:long time1=System.nanoTime();
2.3 或者借助于SimpleDateFormat 將Date格式化到毫秒也可以解決文件重名的問題。
測試。
打開頁面地址如下圖所示:
當(dāng)前名稱:java后臺接受到圖片后保存方法
網(wǎng)站路徑:http://chinadenli.net/article36/jsiepg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)網(wǎng)站制作、網(wǎng)站維護、響應(yīng)式網(wǎng)站、企業(yè)建站、自適應(yīng)網(wǎng)站、靜態(tài)網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)