Anh ơi làm sao để lấy File zip trả ra đó để gửi vào tài liệu khi gửi Mail ạ. Hiện tại em đang xuất fle ra 1 ổ mặc định còn xuất ra file để thêm vào tài liệu đính kèm email thì mình làm cách nào ạ. Code:
public void zipFiles(){ FileOutputStream fos = null; ZipOutputStream zipOut = null; FileInputStream fis = null; try { File folder = new File("D:\\Data\\attachment"); File[] attachFiles = folder.listFiles(); fos = new FileOutputStream("C:\\Users\\admin\\Desktop\\tai-lieu\\testing.zip"); zipOut = new ZipOutputStream(new BufferedOutputStream(fos)); for(File file: attachFiles){ fis = new FileInputStream(file); ZipEntry ze = new ZipEntry(file.getName()); System.out.println("Zipping the file: "+file.getName()); zipOut.putNextEntry(ze); byte[] tmp = new byte[1024]; int size = 0; while((size = fis.read(tmp)) != -1){ zipOut.write(tmp, 0, size); } zipOut.flush(); fis.close(); } zipOut.close(); System.out.println("Done... Zipped the files..."); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally{ try{ if(fos != null) fos.close(); } catch(Exception ex){ } } }
https://stackoverflow.com/questions/23083574/mail-attachments-with-byte-array
Take a look at this
Theo hướng dẫn này, em có thể thấy:
Như vậy, sau khi zip file xong thì em sẽ lưu file ở 1 nơi nào đó, ví dụ của em là
C:\\Users\\admin\\Desktop\\tai-lieu\\testing.zip
vậy em sẽ truyền đường dẫn này vào MimeBodyPart thay cho"path/to/file"
là được em ạ, em hiểu ý anh chứ?Vâng anh em hiểu cách này em đã làm được, em đang tìm cách làm nén file xong lấy luôn được file mà không cần lưu vào đâu.
Có thể dùng cách bạn anaconda875 nói nhé, tuy nhiên với file nhỏ thôi, file hàng MB thì không ổn em ạ, nó sẽ làm hết RAM nếu cứ load hết dữ liệu lên bộ nhớ, anh nghĩ dùng file sẽ hợp lý hơn em ạ.
vâng ang em cảm ơn