最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java實(shí)現(xiàn)下載文件的6種方式

 更新時(shí)間:2023年06月09日 09:09:23   作者:佳德_Boy  
本文主要介紹了Java實(shí)現(xiàn)下載文件的6種方式,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

一、以流的方式下載

public HttpServletResponse download(String path, HttpServletResponse response) {
? ? ? ? try {
? ? ? ? ? ? // path是指欲下載的文件的路徑。
? ? ? ? ? ? File file = new File(path);
? ? ? ? ? ? // 取得文件名。
? ? ? ? ? ? String filename = file.getName();
? ? ? ? ? ? // 取得文件的后綴名。
? ? ? ? ? ? String ext = filename.substring(filename.lastIndexOf(".") + 1).toUpperCase();
? ? ? ? ? ? // 以流的形式下載文件。
? ? ? ? ? ? InputStream fis = new BufferedInputStream(new FileInputStream(path));
? ? ? ? ? ? byte[] buffer = new byte[fis.available()];
? ? ? ? ? ? fis.read(buffer);
? ? ? ? ? ? fis.close();
? ? ? ? ? ? // 清空response
? ? ? ? ? ? response.reset();
? ? ? ? ? ? // 設(shè)置response的Header
? ? ? ? ? ? response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes()));
? ? ? ? ? ? response.addHeader("Content-Length", "" + file.length());
? ? ? ? ? ? OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
? ? ? ? ? ? response.setContentType("application/octet-stream");
? ? ? ? ? ? toClient.write(buffer);
? ? ? ? ? ? toClient.flush();
? ? ? ? ? ? toClient.close();
? ? ? ? } catch (IOException ex) {
? ? ? ? ? ? ex.printStackTrace();
? ? ? ? }
? ? ? ? return response;
? ? }

二、下載本地文件

public void downloadLocal(HttpServletResponse response) throws FileNotFoundException {
? ? ? ? // 下載本地文件
? ? ? ? String fileName = "Operator.doc".toString(); // 文件的默認(rèn)保存名
? ? ? ? // 讀到流中
? ? ? ? InputStream inStream = new FileInputStream("c:/Operator.doc");// 文件的存放路徑
? ? ? ? // 設(shè)置輸出的格式
? ? ? ? response.reset();
? ? ? ? response.setContentType("bin");
? ? ? ? response.addHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
? ? ? ? // 循環(huán)取出流中的數(shù)據(jù)
? ? ? ? byte[] b = new byte[100];
? ? ? ? int len;
? ? ? ? try {
? ? ? ? ? ? while ((len = inStream.read(b)) > 0)
? ? ? ? ? ? ? ? response.getOutputStream().write(b, 0, len);
? ? ? ? ? ? inStream.close();
? ? ? ? } catch (IOException e) {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }
? ? }

三、下載網(wǎng)絡(luò)文件

public void downloadNet(HttpServletResponse response) throws MalformedURLException {
? ? ? ? // 下載網(wǎng)絡(luò)文件
? ? ? ? int bytesum = 0;
? ? ? ? int byteread = 0;
? ? ? ? URL url = new URL("windine.blogdriver.com/logo.gif");
? ? ? ? try {
? ? ? ? ? ? URLConnection conn = url.openConnection();
? ? ? ? ? ? InputStream inStream = conn.getInputStream();
? ? ? ? ? ? FileOutputStream fs = new FileOutputStream("c:/abc.gif");
? ? ? ? ? ? byte[] buffer = new byte[1204];
? ? ? ? ? ? int length;
? ? ? ? ? ? while ((byteread = inStream.read(buffer)) != -1) {
? ? ? ? ? ? ? ? bytesum += byteread;
? ? ? ? ? ? ? ? System.out.println(bytesum);
? ? ? ? ? ? ? ? fs.write(buffer, 0, byteread);
? ? ? ? ? ? }
? ? ? ? } catch (FileNotFoundException e) {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? } catch (IOException e) {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }
? ? }
public void downLoadFile(HttpServletResponse response, String httpUrl) {
? ? ? ? ServletOutputStream out = null;
? ? ? ? try {
? ? ? ? ? ? //與服務(wù)器建立連接
? ? ? ? ? ? URL url = new URL(httpUrl);
? ? ? ? ? ? URLConnection conn = url.openConnection();
? ? ? ? ? ? InputStream inputStream = conn.getInputStream();
? ? ? ? ? ? try {
? ? ? ? ? ? ? ? //1.設(shè)置文件ContentType類型,這樣設(shè)置,會自動判斷下載文件類型
? ? ? ? ? ? ? ? response.setContentType("multipart/form-data");
? ? ? ? ? ? } catch (Exception e){
? ? ? ? ? ? ? ? e.printStackTrace();
? ? ? ? ? ? }
? ? ? ? ? ? out = response.getOutputStream();
? ? ? ? ? ? // 讀取文件流
? ? ? ? ? ? int len = 0;
? ? ? ? ? ? byte[] buffer = new byte[1024 * 10];
? ? ? ? ? ? while ((len = inputStream.read(buffer)) != -1) {
? ? ? ? ? ? ? ? out.write(buffer, 0, len);
? ? ? ? ? ? }
? ? ? ? ? ? out.flush();
? ? ? ? ? ? out.close();
? ? ? ? ? ? inputStream.close();
? ? ? ? } catch (Exception e){
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }
? ? }

四、在線打開的方式

public void downLoad(String filePath, HttpServletResponse response, boolean isOnLine) throws Exception {
? ? ? ? File f = new File(filePath);
? ? ? ? if (!f.exists()) {
? ? ? ? ? ? response.sendError(404, "File not found!");
? ? ? ? ? ? return;
? ? ? ? }
? ? ? ? BufferedInputStream br = new BufferedInputStream(new FileInputStream(f));
? ? ? ? byte[] buf = new byte[1024];
? ? ? ? int len = 0;
? ? ? ? response.reset(); // 非常重要
? ? ? ? if (isOnLine) { // 在線打開方式
? ? ? ? ? ? URL u = new URL("file:///" + filePath);
? ? ? ? ? ? response.setContentType(u.openConnection().getContentType());
? ? ? ? ? ? response.setHeader("Content-Disposition", "inline; filename=" + f.getName());
? ? ? ? ? ? // 文件名應(yīng)該編碼成UTF-8
? ? ? ? } else { // 純下載方式
? ? ? ? ? ? response.setContentType("application/x-msdownload");
? ? ? ? ? ? response.setHeader("Content-Disposition", "attachment; filename=" + f.getName());
? ? ? ? }
? ? ? ? OutputStream out = response.getOutputStream();
? ? ? ? while ((len = br.read(buf)) > 0)
? ? ? ? ? ? out.write(buf, 0, len);
? ? ? ? br.close();
? ? ? ? out.close();
? ? }

五、將文件轉(zhuǎn)換成base64

    /**
     * 將文件轉(zhuǎn)為base64
     */
    public static String getBase64FromFile(File file) throws IOException {
        FileInputStream in = null;
        ByteArrayOutputStream out = null;
        try {
            in = new FileInputStream(file);
            out = new ByteArrayOutputStream();
            int read = 0;
            byte[] buffer = new byte[1024];
            while ((read = in.read(buffer, 0, 1024)) != -1) {
                out.write(buffer, 0, read);
            }
            return Base64.getEncoder().encodeToString(out.toByteArray());
        } catch (IOException e) {
            throw e;
        } finally {
            if (in != null) {
                in.close();
            }
            if (out != null){
                out.close();
            }
        }
    }

六、將MultipartFile轉(zhuǎn)換為File

    /**
     * 將MultipartFile轉(zhuǎn)換為File
     */
    public static File MultipartFileToFile(MultipartFile multiFile) throws IOException {
        String fileName = multiFile.getOriginalFilename();
        String prefix = fileName.substring(fileName.lastIndexOf("."));
        InputStream in = null;
        OutputStream out = null;
        try {
            File file = File.createTempFile(fileName, prefix);
            out = new FileOutputStream(file);
            in = multiFile.getInputStream();
            int read = 0;
            byte[] buffer = new byte[1024];
            while ((read = in.read(buffer, 0, 1024)) != -1) {
                out.write(buffer, 0, read);
            }
            return file;
        } catch (Exception e) {
            throw e;
        }finally {
            if (in != null){
                in.close();
            }
            if (out != null){
                out.close();
            }
        }
    }

到此這篇關(guān)于Java實(shí)現(xiàn)下載文件的6種方式的文章就介紹到這了,更多相關(guān)Java 下載文件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java-文件File簡單實(shí)用方法(分享)

    Java-文件File簡單實(shí)用方法(分享)

    下面小編就為大家?guī)硪黄狫ava-文件File簡單實(shí)用方法(分享)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-08-08
  • JAVA面向?qū)ο笾^承?super入門解析

    JAVA面向?qū)ο笾^承?super入門解析

    在JAVA類中使用super來引用父類的成分,用this來引用當(dāng)前對象,如果一個(gè)類從另外一個(gè)類繼承,我們new這個(gè)子類的實(shí)例對象的時(shí)候,這個(gè)子類對象里面會有一個(gè)父類對象。怎么引用里面的父類對象呢?用super來引用,this指當(dāng)前對象的引用,super是當(dāng)前對象里面的父對象的引用
    2022-01-01
  • IDEA使用MyBatisCodeHelperPro來generator代碼的詳細(xì)教程

    IDEA使用MyBatisCodeHelperPro來generator代碼的詳細(xì)教程

    這篇文章主要介紹了IDEA使用MyBatisCodeHelperPro來generator代碼的詳細(xì)教程,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-09-09
  • SpringBoot thymeleaf eclipse熱部署方案操作步驟

    SpringBoot thymeleaf eclipse熱部署方案操作步驟

    今天小編就為大家分享一篇關(guān)于SpringBoot thymeleaf eclipse熱部署方案操作步驟,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧
    2019-03-03
  • java定義數(shù)組的三種類型總結(jié)

    java定義數(shù)組的三種類型總結(jié)

    下面小編就為大家?guī)硪黄猨ava定義數(shù)組的三種類型總結(jié)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2016-10-10
  • Java實(shí)現(xiàn)的串口通信功能示例

    Java實(shí)現(xiàn)的串口通信功能示例

    這篇文章主要介紹了Java實(shí)現(xiàn)的串口通信功能,結(jié)合實(shí)例形式分析了java串口通信的具體操作步驟與相關(guān)注意事項(xiàng),需要的朋友可以參考下
    2018-01-01
  • Shiro:自定義Realm實(shí)現(xiàn)權(quán)限管理方式

    Shiro:自定義Realm實(shí)現(xiàn)權(quán)限管理方式

    這篇文章主要介紹了Shiro:自定義Realm實(shí)現(xiàn)權(quán)限管理方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-10-10
  • java調(diào)用js文件的兩種常用方法示例(支持V8引擎)

    java調(diào)用js文件的兩種常用方法示例(支持V8引擎)

    在Java中調(diào)用JavaScript的方法通常涉及到使用Java的腳本引擎,下面這篇文章主要給大家介紹了關(guān)于java調(diào)用js文件的兩種常用方法(支持V8引擎)的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2024-06-06
  • 運(yùn)行Jar包出現(xiàn)提示xxx中沒有主清單屬性報(bào)錯(cuò)問題解決方法

    運(yùn)行Jar包出現(xiàn)提示xxx中沒有主清單屬性報(bào)錯(cuò)問題解決方法

    這篇文章主要介紹了運(yùn)行Jar包出現(xiàn):xxx中沒有主清單屬性報(bào)錯(cuò),當(dāng)出現(xiàn)報(bào)錯(cuò):xxx中沒有主清單屬性,解決方法也很簡單,在pom.xml配置中,加上相應(yīng)配置即可,需要的朋友可以參考下
    2023-08-08
  • Jaxb2實(shí)現(xiàn)JavaBean與xml互轉(zhuǎn)的方法詳解

    Jaxb2實(shí)現(xiàn)JavaBean與xml互轉(zhuǎn)的方法詳解

    這篇文章主要介紹了Jaxb2實(shí)現(xiàn)JavaBean與xml互轉(zhuǎn)的方法,簡單介紹了JAXB的概念、功能及實(shí)現(xiàn)JavaBean與xml互轉(zhuǎn)的具體操作技巧,需要的朋友可以參考下
    2017-04-04

最新評論

和林格尔县| 贵定县| 洮南市| 弋阳县| 三原县| 章丘市| 炎陵县| 华阴市| 柯坪县| 东台市| 闽侯县| 东莞市| 博湖县| 积石山| 肇东市| 兰考县| 宣恩县| 赣州市| 吉首市| 光泽县| 潜江市| 赣榆县| 清远市| 嵊泗县| 呼伦贝尔市| 原平市| 扎兰屯市| 和顺县| 深水埗区| 阳春市| 宜阳县| 诏安县| 江山市| 高台县| 凤城市| 泽库县| 鸡东县| 固阳县| 甘泉县| 临沭县| 革吉县|