httpclient base64 文件上传_文件上传下载
說(shuō)道文件上傳下載,這個(gè)業(yè)務(wù)需求并不是很復(fù)雜思想如下
????1.將文件上傳到?某臺(tái)服務(wù)器上的指定的路徑下也可以這樣理解
文件上傳就是將本地圖片發(fā)送到別的地方,下載就是將別的地方的圖片放在本地????2.將路徑同文件名等等相關(guān)的信息存儲(chǔ)入庫(kù)''
????3.完成
?思考一下哈:
? ? 1.所謂文件上傳就是文件的一個(gè)復(fù)制過程對(duì)吧
????2.文件在傳輸?shù)倪^程中是以怎樣的形式進(jìn)行傳輸?shù)?/p>眾所周知哈就是以流的形式進(jìn)行傳輸?shù)哪敲戳鞯降资鞘裁?這里我的見解哈流就是二進(jìn)制的字節(jié)數(shù)組byte[]
??? 3.一說(shuō)到流我們就會(huì)想到的是io流對(duì)吧其 基本理論知識(shí) 都是死的就是流的分類,怎樣使用這些實(shí)體類,怎樣更快應(yīng)為我是初級(jí),所以我的理解就在這里。見笑了哈哈。
?代碼展示一下
public static void main(String[] args) { String name = "aaa"; try { InputStream is = new FileInputStream ("D:\\c.jpg"); OutputStream os = new FileOutputStream ("D:\\DaiMa\\2020914203533\\src\\com\\zkp\\test\\controller\\image\\"+name+".jpg"); byte[] buffer = new byte[1024]; int len = 0; while((len=is.read(buffer))>0){ os.write(buffer,0,len); } is.close(); os.close(); System.out.println("文件上傳成功"); } catch (Exception e) { System.out.println( "文件上傳失敗"); e.printStackTrace(); }}上邊代碼沒啥技術(shù)含量但就是想展示一下哈哈,咱們得一步一步走
圖片上傳需要和前端進(jìn)行配合,這里我們就需要進(jìn)行接收前端傳來(lái)的圖片數(shù)據(jù)
前端圖片數(shù)據(jù)的類型:
Multiplefile? ? 或者是Multiplefile[]
base64
Multiplefile具體實(shí)現(xiàn):
@RequestMapping(value = "/addPhoto",produces = "text/html;charset=utf-8")@ResponseBodypublic String upFile(String name , MultipartFile fil , ModelMap map, HttpServletRequest request) throws IOException { //一.進(jìn)行接收我們前邊傳來(lái)的參數(shù) //1.獲取文件的名字 String name1 = fil.getName(); //2.進(jìn)行獲取大小 long size = fil.getSize(); //3.進(jìn)行獲取類型 String contentType = fil.getContentType(); //4.進(jìn)行獲取文件全名 String originalFilename = fil.getOriginalFilename(); //5.進(jìn)行全部輸出 System.out.println("名字1"+name1); System.out.println("大小"+size); System.out.println("類型"+contentType); System.out.println("文件 全名"+originalFilename); //進(jìn)行創(chuàng)建文件的存儲(chǔ)的名字,這里我們不能使其出現(xiàn)重復(fù)所以我們使用的是隨機(jī) //文件名:UUID+上傳文件的擴(kuò)展名 String pictureName = UUID.randomUUID().toString() +originalFilename.substring(originalFilename.lastIndexOf(".")); System.out.println("文件的新名字"+pictureName); //System.out.println(pictureName); map.put("a",name1); map.put("b",size); map.put("c",contentType); map.put("d", originalFilename); map.put("e",pictureName); Pic pic = new Pic(); pic.setNane1(name1); pic.setAllName(pictureName); pic.setName2(name); pic.setSize(size); pic.setType(contentType); try { //文件名 轉(zhuǎn)移轉(zhuǎn)換 新的文件 文件存放的地址 文件名 String path = request.getServletContext().getRealPath("")+"/images"; System.out.println(path); File file = new File(path,"aaa"+originalFilename.substring (originalFilename.lastIndexOf("."))); if (!file.exists()){ file.mkdirs(); } fil.transferTo(file); } catch (IOException e) { e.printStackTrace(); request.setAttribute("success","添加成功"); return "error"; } request.setAttribute("error","添加失敗"); return "okPage";}當(dāng)然也能這樣進(jìn)行實(shí)現(xiàn)
@RequestMapping(name="/upload",produces = "text/html;charset=utf-8")@ResponseBodypublic String upload(MultipartFile file, String username, HttpServletRequest request){//對(duì)象名必須和name相同 System.out.println("username"+username); try { String name = file.getOriginalFilename(); Random random = new Random(); String fileName = System.currentTimeMillis()+""+random.nextInt(10000); file.transferTo(new File("D:/"+fileName)); } catch (IOException e) { e.printStackTrace(); return "上傳失敗"; } return "上傳成功";}base64具體實(shí)現(xiàn)(直接進(jìn)行調(diào)用就可)
/** * 將base64數(shù)據(jù)轉(zhuǎn)為二進(jìn)制數(shù)據(jù)并上傳到指定文件夾 * @param path * @param base64 * @return */ public static boolean base64ToImage(String path,String base64){ boolean flag = true; OutputStream outputStream = null; try { byte[] bytes = new BASE64Decoder().decodeBuffer(base64); for (int i = 0; i < bytes.length; i++) { if(bytes[i]<0){ bytes[i]+=256; } } outputStream = new FileOutputStream(path); /** * 將數(shù)據(jù)寫出 */ outputStream.write(bytes); } catch (IOException e) { e.printStackTrace(); flag=false; }finally { if (outputStream!=null) { try { outputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } return flag; }補(bǔ)充將圖片轉(zhuǎn)換為base64
/** * 將圖片轉(zhuǎn)換為base64 * @return */ public static String imageToBase64( String imagePath ){ /** * 1.將本地圖片轉(zhuǎn)換為字節(jié)數(shù)據(jù) */ InputStream inputStream = null; byte[] buffer = null; try { inputStream = new FileInputStream(imagePath); int count=0; while (count==0){ count = inputStream.available(); } buffer=new byte[count]; inputStream.read(buffer); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally { if(inputStream!=null){ try { inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } String result = new BASE64Encoder().encode(buffer); System.out.println(result); return result; }上邊就是我遇到的常用的方式,還有就是創(chuàng)建文件的時(shí)候
如:
D:\\c.jpg我理解為帶后綴為文件,不帶的為文件夾哈哈,因?yàn)槲沂亲詫W(xué)的,所以好多東西需要自己摸索,見諒了.擴(kuò)展:
添加多個(gè)文件MultipartFile[]
至于前端實(shí)質(zhì)上就是發(fā)送post請(qǐng)求,這個(gè)請(qǐng)求怎樣實(shí)現(xiàn)呢就是form表單提交或者是ajax進(jìn)行發(fā)送。
<form action="addPhoto" method="post" enctype="multipart/form-data"> 用戶姓名:<input type="text" value="" name="name"> <br/> 用戶頭像:<input type="file" value="" name="fil"> <br/> <input type="submit" value="提交"> form>我可能整理的比較亂但是,要是有啥好的建議隨時(shí)溝通
之一ajax進(jìn)行發(fā)送就是使用key-value的方式進(jìn)行實(shí)現(xiàn),value就是圖片轉(zhuǎn)換為base64的值
這里base64就是實(shí)質(zhì)上就是進(jìn)行編碼和進(jìn)行解碼編碼就是將二進(jìn)制的
byte[ ]數(shù)組轉(zhuǎn)換為String類型的字符串,解碼就是將tring類型的字符串轉(zhuǎn)換為byte[ ]類型的二進(jìn)制數(shù)組
總而言之就是數(shù)據(jù)格式的抓換和文件位置的轉(zhuǎn)換,還是天上飛的理論總有落地的實(shí)現(xiàn)
在加點(diǎn):就是前端傳來(lái)數(shù)據(jù),這個(gè)護(hù)具就是base64或者是流(就是二進(jìn)制數(shù)組)后端進(jìn)行接收,前端怎樣傳來(lái)后端怎樣接收,上有政策下有對(duì)策哈哈。????反正我就知道這兩種形勢(shì),你要是有知道的隨時(shí)聯(lián)系我哈哈,咱們互相進(jìn)步有問題加微信聯(lián)系哦!
總結(jié)
以上是生活随笔為你收集整理的httpclient base64 文件上传_文件上传下载的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 整合营销推广该如何做?
- 下一篇: 划分用户故事(user-story)的原