java代码如何实现下载,java教程下载-成都快上网建站

java代码如何实现下载,java教程下载

java怎样实现数据下载功能呢

这是我以前弄的一个下载的模块,里面的pl指的是System.out.println(),

创新互联公司是一家集网站建设,延边朝鲜族企业网站建设,延边朝鲜族品牌网站建设,网站定制,延边朝鲜族网站建设报价,网络营销,网络优化,延边朝鲜族网站推广为一体的创新建站企业,帮助传统企业提升企业形象加强企业竞争力。可充分满足这一群体相比中小企业更为丰富、高端、多元的互联网需求。同时我们时刻保持专业、时尚、前沿,时刻以成就客户成长自我,坚持不断学习、思考、沉淀、净化自己,让我们为更多的企业打造出实用型网站。

详情可以看 ;tid=156

package com.jc.download;

import java.io.BufferedInputStream;

import java.io.File;

import java.io.IOException;

import java.io.RandomAccessFile;

import java.net.HttpURLConnection;

import java.net.URL;

import static com.jc.tool.io.Out.pl;

public class DownloadThread extends Thread {

private String url = null;

private String file = null;

private long offset = 0;

private long length = 0;

private int no = 0;

public DownloadThread(String url, String file, long offset, long length) {

pl("正在初始化下载线程...");

this.url = url;

this.file = file;

this.offset = offset;

this.length = length;

}

public void setNo(int no) {

this.no = no;

}

@Override

public void run() {

try {

pl("线程【"+no+"】开始连接主机...");

HttpURLConnection conn = (HttpURLConnection) new URL(url)

.openConnection();

pl("线程【"+no+"】发送下载请求...");

conn.setRequestMethod("GET");

conn.setRequestProperty("RANGE", "bytes=" + this.offset + "-"

+ (this.offset + this.length - 1));

pl("线程【"+no+"】创建文件流...");

BufferedInputStream bis = new BufferedInputStream(

conn.getInputStream());

byte[] buf = new byte[1024];

int bytesRead;

pl("线程【"+no+"】开始向文件写入数据...");

while ((bytesRead = bis.read(buf, 0, buf.length)) != -1) {

this.writeFile(file, offset, buf, bytesRead);

this.offset += bytesRead;

}

pl("线程【"+no+"】写入完成");

} catch (IOException e) {

e.printStackTrace();

}

pl("线程【"+no+"】退出");

}

public void writeFile(String fileName, long offset, byte[] bytes,

int realLength) throws IOException {

File file = new File(fileName);

RandomAccessFile raf = new RandomAccessFile(file, "rw");

raf.seek(offset);

raf.write(bytes, 0, realLength);

raf.close();

}

}

java下载功能实现

楼主得在后台的控制器中用reponse的输出流转化一下,我给你个例子。

InputStream fis = new BufferedInputStream(new FileInputStream(filePath));byte[] buffer = new byte[fis.available()];fis.read(buffer);fis.close();response.reset();response.addHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes("gbk"),"ISO-8859-1"));response.addHeader("Content-Length", "" + excelFile.length());OutputStream toClient = new BufferedOutputStream(response.getOutputStream());response.setContentType("application/octet-stream");toClient.write(buffer);toClient.flush();toClient.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)

{

Struts1框架 用Java代码实现文件下载 不弹出下载框 代码:

下载代码:

这里我使用的是SpringMVC,不过它在这里的唯一用途就是用来获取ServletContext对象,这个对象的用途,下面实例中有说明

下载,需要用到两个jar包:commons-fileupload.jar和commons-io.jar

Java代码

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.context.ServletContextAware;

import javax.servlet.ServletContext;

import javax.servlet.ServletOutputStream;

import javax.servlet.http.HttpServletResponse;

import java.io.*;

@Controller

public class FileController implements ServletContextAware{

//Spring这里是通过实现ServletContextAware接口来注入ServletContext对象

private ServletContext servletContext;

@RequestMapping("file/download")

public void fileDownload(HttpServletResponse response){

//获取网站部署路径(通过ServletContext对象),用于确定下载文件位置,从而实现下载

String path = servletContext.getRealPath("/");

//1.设置文件ContentType类型,这样设置,会自动判断下载文件类型

response.setContentType("multipart/form-data");

//2.设置文件头:最后一个参数是设置下载文件名(假如我们叫a.pdf)

response.setHeader("Content-Disposition", "attachment;fileName="+"a.pdf");

ServletOutputStream out;

//通过文件路径获得File对象(假如此路径中有一个download.pdf文件)

File file = new File(path + "download/" + "download.pdf");

try {

FileInputStream inputStream = new FileInputStream(file);

//3.通过response获取ServletOutputStream对象(out)

out = response.getOutputStream();

int b = 0;

byte[] buffer = new byte[512];

while (b != -1){

b = inputStream.read(buffer);

//4.写到输出流(out)中

out.write(buffer,0,b);

}

inputStream.close();

out.close();

out.flush();

} catch (IOException e) {

e.printStackTrace();

}

}

@Override

public void setServletContext(ServletContext servletContext) {

this.servletContext = servletContext;

}

}

java中怎么实现下载功能,最好能有代码

private File uploadify;

private String uploadifyFileName;

public String uploadFile1() throws Exception {

String extName = "";// 扩展名

String newFileName = "";// 新文件名

String nowTime = df.format(new Date());// 当前时间

String random = "-" + (Math.round(Math.random() * 9000) + 1000);// 随机函数

String path = "uploads/" + nowTime.substring(0, 6) + "/"

+ nowTime.substring(0, 8) + "/";// 保存路径

String savePath = ServletActionContext.getServletContext().getRealPath(

"");

savePath = savePath.replace("\\", "/");

if (!savePath.substring(savePath.length()).equals("/"))

savePath = savePath + "/";

savePath = savePath + path;

// 获取扩展名

if (uploadifyFileName.lastIndexOf(".") = 0) {

extName = uploadifyFileName.substring(uploadifyFileName

.lastIndexOf("."));

}

newFileName = uploadifyFileName.substring(0,

uploadifyFileName.lastIndexOf("."))

+ nowTime.substring(8) + random + extName;

File file = new File(savePath);

if (!file.exists())

file.mkdirs();

uploadify.renameTo(new File(savePath + newFileName));

/*

* HttpServletResponse response = ServletActionContext.getResponse();

* response.setCharacterEncoding("utf-8");

* response.getWriter().print(uploadifyFileName+"上传成功");

*/

String ctx = Struts2Utils.getRequest().getContextPath();

Struts2Utils.renderText(ctx + "/" + path + newFileName);

Wenjdetail detail = new Wenjdetail();

String pt = path + newFileName;

detail.setName(uploadifyFileName);

detail.setUrl(pt);

wenjdetailManager.saveWenjdetail(detail);

HttpServletResponse response = ServletActionContext.getResponse();

response.setCharacterEncoding("utf-8");

response.getWriter().print("," + detail.getId());

return null; // 这里不需要页面转向,所以返回空就可以了

}

JAVA文件下载如何实现

在http协议下,实现下载一般就两种方法,一个采用cont-type="";此种方法为附件的方式下载;;

另一种较简单,就是你只需要点下载按钮然后跳转到服务器的那个文件路劲就可以了,浏览器自动回进行下载..


当前题目:java代码如何实现下载,java教程下载
本文链接:http://kswjz.com/article/hshhsj.html
扫二维码与项目经理沟通

我们在微信上24小时期待你的声音

解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流