httpclient base64 文件上传_文件上传下载
說道文件上傳下載,這個業務需求并不是很復雜思想如下
????1.將文件上傳到?某臺服務器上的指定的路徑下也可以這樣理解
文件上傳就是將本地圖片發送到別的地方,下載就是將別的地方的圖片放在本地????2.將路徑同文件名等等相關的信息存儲入庫''
????3.完成
?思考一下哈:
? ? 1.所謂文件上傳就是文件的一個復制過程對吧
????2.文件在傳輸的過程中是以怎樣的形式進行傳輸的
眾所周知哈就是以流的形式進行傳輸的那么流到底是什么,這里我的見解哈流就是二進制的字節數組byte[]??? 3.一說到流我們就會想到的是io流對吧其 基本理論知識 都是死的就是流的分類,怎樣使用這些實體類,怎樣更快應為我是初級,所以我的理解就在這里。見笑了哈哈。
?代碼展示一下
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(); }}上邊代碼沒啥技術含量但就是想展示一下哈哈,咱們得一步一步走
圖片上傳需要和前端進行配合,這里我們就需要進行接收前端傳來的圖片數據
前端圖片數據的類型:
Multiplefile? ? 或者是Multiplefile[]
base64
Multiplefile具體實現:
@RequestMapping(value = "/addPhoto",produces = "text/html;charset=utf-8")@ResponseBodypublic String upFile(String name , MultipartFile fil , ModelMap map, HttpServletRequest request) throws IOException { //一.進行接收我們前邊傳來的參數 //1.獲取文件的名字 String name1 = fil.getName(); //2.進行獲取大小 long size = fil.getSize(); //3.進行獲取類型 String contentType = fil.getContentType(); //4.進行獲取文件全名 String originalFilename = fil.getOriginalFilename(); //5.進行全部輸出 System.out.println("名字1"+name1); System.out.println("大小"+size); System.out.println("類型"+contentType); System.out.println("文件 全名"+originalFilename); //進行創建文件的存儲的名字,這里我們不能使其出現重復所以我們使用的是隨機 //文件名:UUID+上傳文件的擴展名 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 { //文件名 轉移轉換 新的文件 文件存放的地址 文件名 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";}當然也能這樣進行實現
@RequestMapping(name="/upload",produces = "text/html;charset=utf-8")@ResponseBodypublic String upload(MultipartFile file, String username, HttpServletRequest request){//對象名必須和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具體實現(直接進行調用就可)
/** * 將base64數據轉為二進制數據并上傳到指定文件夾 * @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); /** * 將數據寫出 */ outputStream.write(bytes); } catch (IOException e) { e.printStackTrace(); flag=false; }finally { if (outputStream!=null) { try { outputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } return flag; }補充將圖片轉換為base64
/** * 將圖片轉換為base64 * @return */ public static String imageToBase64( String imagePath ){ /** * 1.將本地圖片轉換為字節數據 */ 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; }上邊就是我遇到的常用的方式,還有就是創建文件的時候
如:
D:\\c.jpg我理解為帶后綴為文件,不帶的為文件夾哈哈,因為我是自學的,所以好多東西需要自己摸索,見諒了.擴展:
添加多個文件MultipartFile[]
至于前端實質上就是發送post請求,這個請求怎樣實現呢就是form表單提交或者是ajax進行發送。
<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>我可能整理的比較亂但是,要是有啥好的建議隨時溝通
之一ajax進行發送就是使用key-value的方式進行實現,value就是圖片轉換為base64的值
這里base64就是實質上就是進行編碼和進行解碼編碼就是將二進制的
byte[ ]數組轉換為String類型的字符串,解碼就是將tring類型的字符串轉換為byte[ ]類型的二進制數組
總而言之就是數據格式的抓換和文件位置的轉換,還是天上飛的理論總有落地的實現
在加點:就是前端傳來數據,這個護具就是base64或者是流(就是二進制數組)后端進行接收,前端怎樣傳來后端怎樣接收,上有政策下有對策哈哈。????反正我就知道這兩種形勢,你要是有知道的隨時聯系我哈哈,咱們互相進步有問題加微信聯系哦!
總結
以上是生活随笔為你收集整理的httpclient base64 文件上传_文件上传下载的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 整合营销推广该如何做?
- 下一篇: 划分用户故事(user-story)的原