在JAVA項(xiàng)目中怎么根據(jù)Url把文件打包成ZIP?相信很多沒(méi)有經(jīng)驗(yàn)的人對(duì)此束手無(wú)策,為此本文總結(jié)了問(wèn)題出現(xiàn)的原因和解決方法,通過(guò)這篇文章希望你能解決這個(gè)問(wèn)題。
10年積累的網(wǎng)站設(shè)計(jì)、成都網(wǎng)站建設(shè)經(jīng)驗(yàn),可以快速應(yīng)對(duì)客戶對(duì)網(wǎng)站的新想法和需求。提供各種問(wèn)題對(duì)應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識(shí)你,你也不認(rèn)識(shí)我。但先網(wǎng)站設(shè)計(jì)后付款的網(wǎng)站建設(shè)流程,更有克井免費(fèi)網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。
壓縮文件代碼工具類:
public class UrlFilesToZip { private static final Logger logger = LoggerFactory.getLogger(UrlFilesToZip.class); //根據(jù)文件鏈接把文件下載下來(lái)并且轉(zhuǎn)成字節(jié)碼 public byte[] getImageFromURL(String urlPath) { byte[] data = null; InputStream is = null; HttpURLConnection conn = null; try { URL url = new URL(urlPath); conn = (HttpURLConnection) url.openConnection(); conn.setDoInput(true); // conn.setDoOutput(true); conn.setRequestMethod("GET"); conn.setConnectTimeout(6000); is = conn.getInputStream(); if (conn.getResponseCode() == 200) { data = readInputStream(is); } else { data = null; } } catch (MalformedURLException e) { logger.error("MalformedURLException", e); } catch (IOException e) { logger.error("IOException", e); } finally { try { if (is != null) { is.close(); } } catch (IOException e) { logger.error("IOException", e); } conn.disconnect(); } return data; } public byte[] readInputStream(InputStream is) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int length = -1; try { while ((length = is.read(buffer)) != -1) { baos.write(buffer, 0, length); } baos.flush(); } catch (IOException e) { logger.error("IOException", e); } byte[] data = baos.toByteArray(); try { is.close(); baos.close(); } catch (IOException e) { logger.error("IOException", e); } return data; } }
控制層代碼:
public void filesdown(HttpServletResponse response){ try { String filename = new String("xx.zip".getBytes("UTF-8"), "ISO8859-1");//控制文件名編碼 ByteArrayOutputStream bos = new ByteArrayOutputStream(); ZipOutputStream zos = new ZipOutputStream(bos); UrlFilesToZip s = new UrlFilesToZip(); int idx = 1; for (String oneFile : urls) { zos.putNextEntry(new ZipEntry("profile" + idx); byte[] bytes = s.getImageFromURL(oneFile); zos.write(bytes, 0, bytes.length); zos.closeEntry(); idx++; } zos.close(); response.setContentType("application/force-download");// 設(shè)置強(qiáng)制下載不打開(kāi) response.addHeader("Content-Disposition", "attachment;fileName=" + filename);// 設(shè)置文件名 OutputStream os = response.getOutputStream(); os.write(bos.toByteArray()); os.close(); } catch (FileNotFoundException ex) { logger.error("FileNotFoundException", ex); } catch (Exception ex) { logger.error("Exception", ex); } } }
注意:
1. String filename = new String(“xx.zip”.getBytes(“UTF-8”), “ISO8859-1”);
包裝zip文件名不發(fā)生亂碼。
2.一定要注意,否則會(huì)發(fā)生下載下來(lái)的壓縮包無(wú)法解壓。在給OutputStream 傳值之前,一定要先把ZipOutputStream的流給關(guān)閉了!
看完上述內(nèi)容,你們掌握在JAVA項(xiàng)目中怎么根據(jù)Url把文件打包成ZIP的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!