`
sun113
  • 浏览: 51621 次
  • 来自: NeverNeverlaNd
最近访客 更多访客>>
文章分类
社区版块
存档分类
最新评论

DownloadUtil

阅读更多
//只需要将文件写到servlet输出流中就可以了
package util;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletResponse;

public class DownloadUtil {

    private HttpServlet servlet;

    private HttpServletResponse response;

    public DownloadUtil(HttpServlet servlet, HttpServletResponse response) {
        this.servlet = servlet;
        this.response = response;
    }

    /**
     * this method will produce a query window to query
     * where this file should be stored
     */
    public void produceQuiryWindow() {
        String mainpath = servlet.getServletContext().getRealPath("/");

        String srcpath = mainpath + "\\WEB-INF\\src\\sourceFile\\s.pdf";

        System.out.println(srcpath);

        response.reset();
        //set MIME type, tell the browser how to process it
        response.setContentType("application/pdf");
        // excel file will use this content type
        // response.setContentType("application/ms-excel");
        // MIME type for MSWord doc
        // response.setContentType( "application/msword" );
        // MIME type for csv
        // response.setContentType("application/x-csv");
       
        // set file type information, and how to open it
        response.setHeader("Content-Disposition",
                "attachment;filename=news.pdf");
        InputStream is = null;
        OutputStream os = null;
        try {
            is = new FileInputStream(srcpath);

            os = response.getOutputStream();

            int len = 0;
            byte[] b = new byte[1024];

            while ((len = is.read(b)) > 0) {
                os.write(b, 0, len);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            try{
                os.close();
                is.close();
            }catch(IOException e){
                e.printStackTrace();
            }
           
        }

    }

    /**
     * no query window copy the file from the sourceFile folder to the
     * targetFile foler
     */
    public void copytonewfolder() {
        String mainpath = servlet.getServletContext().getRealPath("/");

        String srcpath = mainpath + "\\WEB-INF\\src\\sourceFile\\s.pdf";

        System.out.println(srcpath);
       
        InputStream is = null;
        OutputStream os2 = null;
        try {
            is = new FileInputStream(srcpath);

            String targetPath = mainpath
                    + "\\WEB-INF\\src\\targetFile\\newS.pdf";
            os2 = new FileOutputStream(targetPath);

            int len = 0;
            byte[] b = new byte[1024];

            while ((len = is.read(b)) > 0) {
                os2.write(b);
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            try{
                os2.close();
                is.close();
            }catch(IOException e){
                e.printStackTrace();
            }

        }

    }

}
分享到:
评论

相关推荐

    DownLoadUtil.java

    public class DownLoadUtil { public static String getFileName(String agent, String filename) throws UnsupportedEncodingException { if (agent.contains("MSIE")) { // IE浏览器 ...

    DownloadUtil(JAVA下载网络文件,解决路径或文件名含中文问题).java

    JAVA下载网络文件,解决路径或文件名含中文问题的工具类!

    Android傻瓜式下载工具DownloadUtil.zip

    标签:DownloadUtil 分享 window._bd_share_config = { "common": { "bdSnsKey": {}, "bdText": "", "bdMini": "2", "bdMiniList": [], "bdPic...

    带进度的progressUtil

    `DownloadUtil.java`这个文件很可能是ProgressUtil的核心实现,包含了一个或多个方法,用于处理下载任务并提供进度更新。在Android中,文件下载通常通过`java.net.URL`、`java.io`和`AsyncTask`等API来实现。`...

    内网文件互传工具,包含PC端(.jar)及安卓端(.apk)

    此压缩包内包含一个 DownloadUtil.jar 是用于PC端运行(需PC有java运行环境),以及 FileUploadDownload.apk 是用于安卓手机。 原理就是使用Socket通信,其中一端启动服务器,另一端访问服务器,获取档案列表进行...

    上传下载utils

    `DownLoadUtil.java` 和 `UploadUtil.java` 是两个实用的工具类,它们为开发者提供了便捷的方式来处理文件的上传与下载任务。下面我们将详细探讨这两个工具类可能实现的功能以及相关的技术知识点。 首先,`...

    Java上传下载excel、解析Excel、生成Excel的问题.docx

    在提供的代码示例中,`downLoadExcelModel` 方法首先获取到文件的服务器路径,并通过`DownLoadUtil`工具类的`downLoadFile`方法进行下载处理。`DownLoadUtil`中,我们设置了不同的`Content-Type`以适应不同类型的...

    RetrofitDownload.rar

    在`RetrofitDownload-master`这个项目中,可能包含了一个名为`DownloadUtil`的工具类,它可能包含了以下功能: 1. 创建Retrofit实例。 2. 定义下载服务接口。 3. 提供下载文件的方法,接收文件名作为参数。 4. 在回...

    Java多线程下载网络图片

    在这个案例中,可能`ThreadDownload.java`和`DownLoadUtil.java`分别代表了这两个概念的应用。 `ThreadDownload.java`可能是定义了一个新的线程类,继承自`Thread`。类中重写了`run()`方法,这是线程执行的主要逻辑...

    xutil 安卓框架 教程

    通过调用DownloadUtil类的相关方法,可以轻松实现后台下载任务,并支持断点续传和多线程下载。只需传入URL、保存路径和回调接口,框架会自动处理下载过程,开发者无需关心底层实现。 四、中文缓存 在Android应用中...

    ssh常用工具类

    4. **DownloadUtil.java**:这个类专门处理文件下载功能,可能包含HTTP或FTP下载方法,进度监控,断点续传等功能。在Web应用中,用户下载文件时,这样的工具类能提供便捷的下载管理。 5. **MailUtil.java**:邮件...

    文件断点续传

    在提供的`DownloadUtil`文件中,可能包含了实现上述功能的代码,包括创建下载任务、处理HTTP请求、多线程下载、进度存储和文件合并等功能。具体实现细节需要查看源代码才能深入了解。 总之,Android上的文件断点续...

    Android启动动画大全

    在Android开发中,启动动画(Splash Screen)是用户打开应用时首先看到的界面,它通常展示品牌标识或应用的简短介绍,同时加载必要的资源和数据。本资源"Android启动动画大全"似乎提供了一系列用于创建不同启动效果...

    LoadApkAndInstallLib:APK下载安装第三方库,已上传至jitpack.io

    repositories { jcenter() maven { url 'https://jitpack.io' } }}app Gradle添加compile 'com.github.jasonsyf:LoadApkAndInstallLib:v0.2.5'用法DownLoadUtil.insrance.startInstall(MainActivity.this, APK_...

Global site tag (gtag.js) - Google Analytics