「java流下载」java的下载

博主:adminadmin 2022-11-27 11:35:10 74

本篇文章给大家谈谈java流下载,以及java的下载对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

Java实现流下载,浏览器没有反应 如何解决?

是否加了 attchment 的头信息?

或者检查浏览器是否阻止了弹出窗口。

用java流的方式怎么指定下载到指定目录下

举例代码:

    /**

     * 下载文件。

     * @param urlStr 文件的URL

     * @param savePath 保存到的目录

     * @param fileName 保存的文件名称

     * @param description 描述(如:歌曲,专辑封面,歌词等)

     * @throws IOException

     */

    public static void downLoad(String urlStr, String savePath, String fileName, String description) throws IOException

    {

        URL url = new URL(urlStr);

        HttpURLConnection conn = (HttpURLConnection) url.openConnection();

        conn.setConnectTimeout(100000);  // 设置超时间为10秒

        conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");  // 防止屏蔽程序抓取而返回403错误

        File saveDir = new File(savePath);

        File file = new File(saveDir + File.separator + fileName);

        try (InputStream inputStream = conn.getInputStream();

                FileOutputStream fos = new FileOutputStream(file))

        {

            byte[] flowData = readInputStream(inputStream);

            fos.write(flowData);

        } catch (Exception e) {

            MainFrame.logEvent("字节保存时出现意外:" + e.getMessage());

        }

        MainFrame.logEvent(description + "下载完成:" + url);

    }

MainFrame.logEvent()是我自己弄的日志记录而已,可以忽略不看

java怎么使用流读取远程的文件并下载到本地

使用TCP|UDP进行网络传输 。或则使用使用FIle对象给定一个远程的路径都可以

Java实现文件流下载文件,浏览器无反应,后台无错误!如何解决?

ServletOutputStream out = response.getOutputStream();

byte[] outputByte = new byte[4096];

while(text.read(outputByte, 0, 4096) != -1)

{

out.write(outputByte, 0, 4096);

}

fileIn.close();

out.flush();

out.close();

怎样编一个能实现文件下载功能的JAVA程序

java实现文件下载

一、采用RequestDispatcher的方式进行

1、web.xml文件中增加

mime-mapping

extensiondoc/extension

mime-typeapplication/vnd.ms-word/mime-type

/mime-mapping

2、程序如下:

%@page language="java" import="java.net.*" pageEncoding="gb2312"%

%

response.setContentType("application/x-download");//设置为下载application/x-download

String filenamedownload = "/系统解决方案.doc";//即将下载的文件的相对路径

String filenamedisplay = "系统解决方案.doc";//下载文件时显示的文件保存名称

filenamedisplay = URLEncoder.encode(filenamedisplay,"UTF-8");

response.addHeader("Content-Disposition","attachment;filename=" + filenamedisplay);

try

{

RequestDispatcher dispatcher = application.getRequestDispatcher(filenamedownload);

if(dispatcher != null)

{

dispatcher.forward(request,response);

}

response.flushBuffer();

}

catch(Exception e)

{

e.printStackTrace();

}

finally

{

}

%

二、采用文件流输出的方式下载

1、web.xml文件中增加

mime-mapping

extensiondoc/extension

mime-typeapplication/vnd.ms-word/mime-type

/mime-mapping

2、程序如下:

%@page language="java" contentType="application/x-msdownload" import="java.io.*,java.net.*" pageEncoding="gb2312"%

%

//关于文件下载时采用文件流输出的方式处理:

//加上response.reset(),并且所有的%后面不要换行,包括最后一个;

//因为Application Server在处理编译jsp时对于%和%之间的内容一般是原样输出,而且默认是PrintWriter,

//而你却要进行流输出:ServletOutputStream,这样做相当于试图在Servlet中使用两种输出机制,

//就会发生:getOutputStream() has already been called for this response的错误

//详细请见《More Java Pitfill》一书的第二部分 Web层Item 33:试图在Servlet中使用两种输出机制 270

//而且如果有换行,对于文本文件没有什么问题,但是对于其它格式,比如AutoCAD、Word、Excel等文件

//下载下来的文件中就会多出一些换行符0x0d和0x0a,这样可能导致某些格式的文件无法打开,有些也可以正常打开。

response.reset();//可以加也可以不加

response.setContentType("application/x-download");//设置为下载application/x-download

// /../../退WEB-INF/classes两级到应用的根目录下去,注意Tomcat与WebLogic下面这一句得到的路径不同,WebLogic中路径最后没有/

System.out.println(this.getClass().getClassLoader().getResource("/").getPath());

String filenamedownload = this.getClass().getClassLoader().getResource("/").getPath() + "/../../系统解决方案.doc";

String filenamedisplay = "系统解决方案.doc";//系统解决方案.txt

filenamedisplay = URLEncoder.encode(filenamedisplay,"UTF-8");

response.addHeader("Content-Disposition","attachment;filename=" + filenamedisplay);

OutputStream output = null;

FileInputStream fis = null;

try

{

output = response.getOutputStream();

fis = new FileInputStream(filenamedownload);

byte[] b = new byte[1024];

int i = 0;

while((i = fis.read(b)) 0)

{

output.write(b, 0, i);

}

output.flush();

}

catch(Exception e)

{

System.out.println("Error!");

e.printStackTrace();

}

finally

{

if(fis != null)

{

关于Java用输出流下载文件

通常可以直接通过FTPClient 工具类下载文件到本地的,可以通过ByteArrayOutputStream进行流的读取输出。

/**

*下载文件

*

* @param fileName

* @param date

* @param plainFilePath 明文文件路径路径

* @param filepath

* @return

* @throws Exception

*/

public static String encodeAESFileDownloadByFtp(String plainFilePath, String fileName, String date,String filepath) throws Exception {

FileInputStream fis = null;

ByteArrayOutputStream bos = null;

FileOutputStream fos = null;

FTPClient ftpClient = new FTPClient();

String bl = "false";

try {

// Log.info("下载并解密文件开始");

Log.info("连接远程下载服务器"+CMBCUtil.CMBCHOSTNAME+":"+2021);

ftpClient.connect(CMBCUtil.CMBCHOSTNAME, 2021);

ftpClient.login(CMBCUtil.CMBCLOGINNAME, CMBCUtil.CMBCLOGINPASSWORD);

FTPFile[] fs;

fs = ftpClient.listFiles();

for (FTPFile ff : fs) {

if (ff.getName().equals(filepath)) {

ftpClient.changeWorkingDirectory("/"+filepath+"/");

}

}

fs = ftpClient.listFiles();

for (FTPFile ff : fs) {

if (ff.getName().equals(date)) {

bl = "true";

ftpClient.changeWorkingDirectory("/"+filepath+"/" + date);

}

}

Log.info("检查文件路径是否存在:/"+filepath+"/"+date + " "+ bl);

if("false".equals(bl)){

ViewUtil.dataSEErrorPerformedCommon( "查询文件路径不存在:"+"/"+filepath+"/" + date);

return bl;

}

bl = "false";

fs = ftpClient.listFiles();

for (FTPFile ff : fs) {

if (ff.getName().equals(fileName)) {

bl = "true";

Log.info("下载并解密文件开始。");

fos = new FileOutputStream(plainFilePath);

ftpClient.setBufferSize(1024);

// 设置文件类型(二进制)

ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);

InputStream is = ftpClient.retrieveFileStream(fileName);

bos = new ByteArrayOutputStream(is.available());

byte[] buffer = new byte[1024];

int count = 0;

while ((count = is.read(buffer)) != -1) {

bos.write(buffer, 0, count);

}

bos.flush();

byte[] bytes = decodeAES(key, bos.toByteArray());

fos = new FileOutputStream(plainFilePath);

fos.write(bytes);

Log.info("下载并解密文件结束:"+plainFilePath);

}

}

Log.info("检查文件是否存:"+fileName+" "+bl);

if("false".equals(bl)){

ViewUtil.dataSEErrorPerformedCommon("查询无结果,请稍后再查询。");

return bl;

}

return bl;

} catch (Exception e) {

throw e;

} finally {

if (fis != null) {

try {

fis.close();

} catch (Exception e) {

Log.info(e.getLocalizedMessage(), e);

}

}

if (bos != null) {

try {

bos.close();

} catch (Exception e) {

Log.info(e.getLocalizedMessage(), e);

}

}

if (fos != null) {

try {

fos.close();

} catch (Exception e) {

Log.info(e.getLocalizedMessage(), e);

}

}

}

}

关于java流下载和java的下载的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。

The End

发布于:2022-11-27,除非注明,否则均为首码项目网原创文章,转载请注明出处。