导航:首页 > 编程语言 > java下载文件到本地

java下载文件到本地

发布时间:2022-06-28 21:39:09

javaWeb如何将文件下载到本地.要求不要有提示下载框的,直接点击后,就下载到某个本地盘下。

点击后转向执行的方法:先获取点击的文件路径,然后通过读取文件的IO流对象,放到缓冲流里面,然后向网络传输文件流。

② java下载bat文件在本地运行的办法

=Runtime.getRuntime().exec("cmd/crun.bat");//要执行的文件的路径为run.bat//得到输入流InputStreaminputStream=process.getInputStream();=newInputStreamReader(inputStream);BufferedReaderbufferedReader=newBufferedReader(inputStreamReader);//得到输出流OutputStreamoutputStream=process.getOutputStream();=newOutputStreamWriter(outputStream);BufferedWriterbufferedWriter=newBufferedWriter(outputStreamWriter);bufferedWriter.write("dd
");bufferedWriter.flush();Stringtemp=null;while((temp=bufferedReader.readLine())!=null){System.out.println(temp);}}

③ java代码实现从svn服务器下载文件到本地

首先你要安装svn客户端,安装完成以后你右键选择svn中的import,输入你服务器端代码的地址,下载路径什么的自己配置,其他不用管,点击OK就可以了,不过你要有read权限才行。

④ Java如何利用url下载MP3保存到本地

Java如何利用url下载MP3保存的方法:

1 /** ;

2 * TODO 下载文件到本地 ;

3 * @author nadim ;
4 * @date Sep 11, 2015 11:45:31 AM ;

5 * @param fileUrl 远程地址 ;

6 * @param fileLocal 本地路径 ;

7 * @throws Exception ;
8 */ ;

9 public void downloadFile(String fileUrl,String fileLocal) throws Exception {;

10 URL url = new URL(fileUrl);

11 HttpURLConnection urlCon = (HttpURLConnection) url.openConnection();

12 urlCon.setConnectTimeout(6000);

13 urlCon.setReadTimeout(6000);

14 int code = urlCon.getResponseCode();

15 if (code != HttpURLConnection.HTTP_OK) {

16 throw new Exception("文件读取失败");

17 }

18 //读文件流;

19 DataInputStream in = new DataInputStream(urlCon.getInputStream());

20 DataOutputStream out = new DataOutputStream(new FileOutputStream(fileLocal));

21 byte[] buffer = new byte[2048];

22 int count = 0;

23 while ((count = in.read(buffer)) > 0) {;

24 out.write(buffer, 0, count);

25 }

26 out.close();

27 in.close();

28 }。

Java是一门面向对象编程语言,不仅吸收了C++语言的各种优点,还摒弃了C++里难以理解的多继承、指针等概念,因此Java语言具有功能强大和简单易用两个特征。

Java语言作为静态面向对象编程语言的代表,极好地实现了面向对象理论,允许程序员以优雅的思维方式进行复杂的编程 。

⑤ Java 下载文件的方法怎么写

参考下面
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();
// 设置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(); // 文件的默认保存名
// 读到流中
InputStream inStream = new FileInputStream("c:/Operator.doc");// 文件的存放路径
// 设置输出的格式
response.reset();
response.setContentType("bin");
response.addHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
// 循环取出流中的数据
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();
}
}

// 下载网络文件
public void downloadNet(HttpServletResponse response) throws MalformedURLException {
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 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());
// 文件名应该编码成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();
}

⑥ java 将页面内容写入excel文件中并可以将其下载到本地任意位置

java本身要生成excel文件必然是在后台做的,通过poi库生成excel文件并制作表格。
无法直接通过网页保存生成excel。
至于下载到本地任意位置,也是后台生成了excel文件发送到前台(浏览器),由用户选择要存在哪儿,不能直接存储(这是web沙箱限制,不允许网页直接访问本地硬盘,不然你想想,如果你打开一个网页,网页代码可以任意访问你的硬盘,你还敢开网页吗)。
要绕过沙箱限制必须装插件,也就是,你必须开发一个com或plugin插件,可以访问本地硬盘,但这需要用户手工安装(比如flash的插件,你之所以能用网页看flash是因为装了它的插件,但这是你手工装的,它不能绕过你直接给你装,它必须询问你行不行,你要手工点了OK,才能装)

⑦ JAVA下载tomcat目录下的文件,/home/tomcat目录下有个1.txt文件,如何把这个文件给下载到本地

如果下载的文件在项目工程外,可以建立一个下载的action,该action以输入流的方式读取文件,并以输出流的方式写到前台,前台页面点击下载时调用该action即可。

⑧ java实现从服务器下载tif文件到本地

不要考虑文件格式,你把文件以流的方式读入在下载到本地就可以了

⑨ java下载服务器上的文件到客户端

java编程方法下载服务器上的文件到本地客服端,代码如下:

importjava.io.BufferedWriter;
importjava.io.File;
importjava.io.FileOutputStream;
importjava.io.FileWriter;
importjava.io.IOException;
importjava.io.InputStream;
importjava.net.URL;
importjava.net.URLConnection;

publicclassDownLoad{
publicstaticvoiddownloadFile(URLtheURL,StringfilePath)throwsIOException{
FiledirFile=newFile(filePath);
if(!dirFile.exists()){
//文件路径不存在时,自动创建目录
dirFile.mkdir();
}
//从服务器上获取图片并保存
URLConnectionconnection=theURL.openConnection();
InputStreamin=connection.getInputStream();
FileOutputStreamos=newFileOutputStream(filePath+"\123.png");
byte[]buffer=newbyte[4*1024];
intread;
while((read=in.read(buffer))>0){
os.write(buffer,0,read);
}
os.close();
in.close();
}
publicstaticvoidmain(String[]args){
//下面添加服务器的IP地址和端口,以及要下载的文件路径
StringurlPath="http://服务器IP地址:端口/image/123.png";

//下面代码是下载到本地的位置
StringfilePath="d:\excel";

URLurl=newURL(urlPath);

try{

downloadFile(url,filePath);

}catch(IOExceptione){

e.printStackTrace();

}

}

}

⑩ java 如何下载文件

httpURLConnection conn;
conn.getInputStream;
再将这个stream 写到文件就可以了

阅读全文

与java下载文件到本地相关的资料

热点内容
成都市区建成面积算法 浏览:656
智能家居单片机 浏览:93
买男装用什么app好 浏览:851
文件夹合并了怎么拆开 浏览:256
波段副图源码无未来函数 浏览:84
livecn服务器地址 浏览:257
程序员这个工作真的很吃香吗 浏览:844
程序员和数学分析师待遇 浏览:678
压缩气弹簧怎么拆 浏览:321
华为公有云服务器添加虚拟ip 浏览:209
程序员和运营哪个累 浏览:24
抖音安卓信息提示音怎么设置 浏览:454
光速虚拟机的共享文件夹 浏览:248
程序员培训机构发的朋友圈真实性 浏览:742
天干地支简单算法 浏览:299
下载个压缩文件 浏览:300
普通人电脑关机vs程序员关机 浏览:628
米酷建站源码 浏览:115
氢气app怎么搜搭配 浏览:619
pdf绿盟 浏览:505