欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

java后臺(tái)接受到圖片后保存方法

Java是一門面向?qū)ο缶幊陶Z(yǔ)言,不僅吸收了C++語(yǔ)言的各種優(yōu)點(diǎn),還摒棄了C++里難以理解的多繼承、指針等概念,因此Java語(yǔ)言具有功能強(qiáng)大和簡(jiǎn)單易用兩個(gè)特征。Java語(yǔ)言作為靜態(tài)面向?qū)ο缶幊陶Z(yǔ)言的代表,極好地實(shí)現(xiàn)了面向?qū)ο罄碚摚试S程序員以優(yōu)雅的思維方式進(jìn)行復(fù)雜的編程  。

成都創(chuàng)新互聯(lián)公司成立與2013年,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目成都做網(wǎng)站、網(wǎng)站建設(shè)網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢(mèng)想脫穎而出為使命,1280元南鄭做網(wǎng)站,已為上家服務(wù),為南鄭各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話:18982081108

Java具有簡(jiǎn)單性、面向?qū)ο蟆⒎植际健⒔研浴踩浴⑵脚_(tái)獨(dú)立與可移植性、多線程、動(dòng)態(tài)性等特點(diǎn) 。Java可以編寫桌面應(yīng)用程序、Web應(yīng)用程序、分布式系統(tǒng)和嵌入式系統(tǒng)應(yīng)用程序等 。

  • 第一步:借助于springmvc框架的平臺(tái)實(shí)現(xiàn)。
  • 第二步:java網(wǎng)頁(yè)下載功能怎么獲取文件名。
  • 第三步:前端如何實(shí)現(xiàn)突破預(yù)覽效果。

第二步驟:主要功能實(shí)現(xiàn)。springboot默認(rèn)是集成springmvc,使用springboot和直接使用springmvc上傳是一樣的。springboot默認(rèn)是集成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>

 

java后臺(tái)接受到圖片后保存方法

控制器UploadController 實(shí)現(xiàn)

UploadController 主要分為3部分

1.1 調(diào)整頁(yè)面請(qǐng)求goUploadImg

1.2 上傳請(qǐng)求方法uploadImg

1.3 存儲(chǔ)圖片方法uploadFile

@Controllerpublic class UploadController {  

//跳轉(zhuǎn)到上傳文件的頁(yè)面  

@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:同時(shí)需要將上傳圖片的原始文件名和存儲(chǔ)文件名、以及關(guān)聯(lián)id存入一個(gè)數(shù)據(jù)表中。

2.1 將存儲(chǔ)文件名設(shè)置為UUID,避免存儲(chǔ)文件名重復(fù)

public static String getUUID(){

    UUID uuid=UUID.randomUUID();

    String str = uuid.toString(); 

    String uuidStr=str.replace("-", "");

    return uuidStr;

   }

2.2 將存儲(chǔ)文件名按照時(shí)間生成,避免存儲(chǔ)文件名重復(fù)

System.nanoTime() 

該函數(shù)是返回納秒的。1毫秒=1納秒*1000*1000
如:long time1=System.nanoTime();

2.3 或者借助于SimpleDateFormat 將Date格式化到毫秒也可以解決文件重名的問題。

java后臺(tái)接受到圖片后保存方法

測(cè)試。

打開頁(yè)面地址如下圖所示:

java后臺(tái)接受到圖片后保存方法

java后臺(tái)接受到圖片后保存方法

java后臺(tái)接受到圖片后保存方法

當(dāng)前名稱:java后臺(tái)接受到圖片后保存方法
網(wǎng)站路徑:http://www.chinadenli.net/article36/jsiepg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)網(wǎng)站制作網(wǎng)站維護(hù)響應(yīng)式網(wǎng)站企業(yè)建站自適應(yīng)網(wǎng)站靜態(tài)網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

小程序開發(fā)