本文目录一览:
怎样通过java实现服务器上文件下载?
在jsp/servlet中断点/多线程下载文件
%@ page import="java.io.File" %
%@ page import="java.io.IOException" %
%@ page import="java.io.OutputStream" %
%@ page import="java.io.RandomAccessFile" %
%!
public void downloadFile(HttpServletRequest request, HttpServletResponse response, File file) throws IOException {
RandomAccessFile raf = new RandomAccessFile(file, "r");
java.io.FileInputStream fis = new java.io.FileInputStream(raf.getFD());
response.setHeader("Server", "");
response.setHeader("Accept-Ranges", "bytes");
long pos = 0;
long len;
len = raf.length();
if (request.getHeader("Range") != null) {
response.setStatus(HttpServletResponse.SC_PARTIAL_CONTENT);
pos = Long.parseLong(request.getHeader("Range")
.replaceAll("bytes=", "")
.replaceAll("-", "")
);
}
response.setHeader("Content-Length", Long.toString(len - pos));
if (pos != 0) {
response.setHeader("Content-Range", new StringBuffer()
.append("bytes ")
.append(pos)
.append("-")
.append(Long.toString(len - 1))
.append("/")
.append(len)
.toString()
);
}
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", new StringBuffer()
.append("attachment;filename=\"")
.append(file.getName())
.append("\"").toString());
raf.seek(pos);
byte[] b = new byte[2048];
int i;
OutputStream outs = response.getOutputStream();
while ((i = raf.read(b)) != -1) {
outs.write(b, 0, i);
}
raf.close();
fis.close();
}
%
%
String filePath = request.getParameter("file");
filePath = application.getRealPath(filePath);
File file = new File(filePath);
downloadFile(request, response, file);
%
java 下载服务器端文件,路径怎么写
路径就是如:“/user/etc”。
解释:服务器的路径展现形式不是以盘符开始的,而是以“/”开始,之后的路径和windows系统无任何区别,如上面举例的路径,如果想从etc下拿文件,直接“cd /user/etc”之后找到想要的文件,进行下载即可。
java从服务器下载图片怎么讲图片保存到本地的sdcard上
ublic 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;
}
java下载服务器上的文件到客户端
java编程方法下载服务器上的文件到本地客服端,代码如下:
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
public class DownLoad {
public static void downloadFile(URL theURL, String filePath) throws IOException {
File dirFile = new File(filePath);
if(!dirFile.exists()){
//文件路径不存在时,自动创建目录
dirFile.mkdir();
}
//从服务器上获取图片并保存
URLConnection connection = theURL.openConnection();
InputStream in = connection.getInputStream();
FileOutputStream os = new FileOutputStream(filePath+"\\123.png");
byte[] buffer = new byte[4 * 1024];
int read;
while ((read = in.read(buffer)) 0) {
os.write(buffer, 0, read);
}
os.close();
in.close();
}
public static void main(String[] args) {
//下面添加服务器的IP地址和端口,以及要下载的文件路径
String urlPath = "http://服务器IP地址:端口/image/123.png";
//下面代码是下载到本地的位置
String filePath = "d:\\excel";
URL url = new URL(urlPath);
try {
downloadFile(url,filePath);
} catch (IOException e) {
e.printStackTrace();
}
}
}