java如何实现超链接下载
java实现超链接下载方法如下:
创新互联建站主要从事成都网站制作、成都做网站、网页设计、企业做网站、公司建网站等业务。立足成都服务灵武,10多年网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:028-86922220
response.setHeader("Content-disposition","attachment;filename="下载的文件名字);
备注:让response调用setheader方法添加下载的头给客户的浏览器,浏览器收到该头后就会打开相应的下载对话框。
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下载功能实现
楼主得在后台的控制器中用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 写一个action 能实现点击事件后浏览器能自动下载xml文件的功能!!急
方法一:调用浏览器下载文件
下面Action里的代码可以用调用浏览器进行下载xml文件
import java.awt.Desktop;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URI;
import javax.swing.JButton;
import javax.swing.JFrame;
public class DownLoadDemo extends JFrame {
JButton jb;
public DownLoadDemo() {
jb = new JButton("Download");
jb.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
String path = "网络xml的地址";
URI uri = new URI(path);
Desktop.getDesktop().browse(uri);
} catch (Exception e1) {
e1.printStackTrace();
}
}
});
add(jb);
setTitle("调用浏览器下载");
setBounds(380, 380,230,80);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
new DownLoadDemo();
}
}
当时上述下载方式,可能会弹出提示,比如保存位置等.
方法二:使用IO和net下载文件.这种方式没有调用浏览器,下载过程,不会弹出提示灯
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.Date;
public class DownLoadDemo {
public static void main(String[] args) throws Exception {
String str = "XML的网络地址";
URL url = new URL(str);
URLConnection urlconn = url.openConnection();
urlconn.connect();
InputStream is = urlconn.getInputStream();
FileOutputStream fos = new FileOutputStream("qq.exe");
byte[] buf = new byte[4096];
int length = 0;
//System.out.println("开始下载时间:"+new Date());
while((length=is.read(buf))!=-1){
fos.write(buf, 0, length);
}
fos.close();
is.close();
//System.out.println("下载完成时间:"+new Date());
}
}
java 如何实现下载功能
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.ProtocolException;
import java.net.URI;
import java.net.URL;
import java.util.Random;
/**
*
* 实现了下载的功能*/
public class SimpleTh {
public static void main(String[] args){
// TODO Auto-generated method stub
//String path = "倩女幽魂.mp3";//MP3下载的地址
String path ="";
try {
new SimpleTh().download(path, 3); //对象调用下载的方法
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static String getFilename(String path){//获得文件的名字
return path.substring(path.lastIndexOf('/')+1);
}
public void download(String path,int threadsize) throws Exception//下载的方法
{//参数 下载地址,线程数量
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();//获取HttpURLConnection对象
conn.setRequestMethod("GET");//设置请求格式,这里是GET格式
conn.setReadTimeout(5*1000);//
int filelength = conn.getContentLength();//获取要下载文件的长度
String filename = getFilename(path);
File saveFile = new File(filename);
RandomAccessFile accessFile = new RandomAccessFile(saveFile, "rwd");
accessFile.setLength(filelength);
accessFile.close();
int block = filelength%threadsize ==0?filelength/threadsize:filelength/threadsize+1;
for(int threadid = 0;threadid=threadsize;threadid++){
new DownloadThread(url,saveFile,block,threadid).start();
}
}
private final class DownloadThread extends Thread{
private URL url;
private File saveFile;
private int block;//每条线程下载的长度
private int threadid;//线程id
public DownloadThread(URL url,File saveFile,int block,int threadid){
this.url = url;
this.saveFile= saveFile;
this.block = block;
this.threadid = threadid;
}
@Override
public void run() {
//计算开始位置的公式:线程id*每条线程下载的数据长度=?
//计算结束位置的公式:(线程id+1)*每条线程下载数据长度-1=?
int startposition = threadid*block;
int endposition = (threadid+1)*block-1;
try {
try {
RandomAccessFile accessFile = new RandomAccessFile(saveFile, "rwd");
accessFile.seek(startposition);//设置从什么位置写入数据
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setRequestMethod("GET");
conn.setReadTimeout(5*1000);
conn.setRequestProperty("Range","bytes= "+startposition+"-"+endposition);
InputStream inStream = conn.getInputStream();
byte[]buffer = new byte[1024];
int len = 0;
while((len = inStream.read(buffer))!=-1){
accessFile.write(buffer, 0, len);
}
inStream.close();
accessFile.close();
System.out.println("线程id:"+threadid+"下载完成");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
参考一下这个代码。
文章题目:java代码实现下载功能,java 下载功能
分享URL:http://scyingshan.cn/article/dsejhjs.html