java两台服务器之间,大文件上传(续传),采用了Socket通信机制以及JavaIO流两个技术点,具体思路如下:

实现思路:
1、服:利用ServerSocket搭建服务器,开启相应端口,进行长连接操作
2、服:使用ServerSocket.accept()方法进行阻塞,接收客户端请求
3、服:每接收到一个Socket就建立一个新的线程来处理它
4、客:利用Socket进行远程连接,询问已上传进度
5、客:使用FileInputStream.skip(long length)从指定位置读取文件,向服务器发送文件流
6、服:接收客户端输入流,使用RandomAccessFile.seek(long length)随机读取,将游标移动到指定位置进行读写
7、客/服:一个循环输出,一个循环读取写入
8、示例:以下是具体代码,仅供参考
文件介绍:
FileUpLoadServer.java(服务器接收文件类)
FileUpLoadClient.java(客户端发送文件类)
FinalVariables.java(自定义参数类)
SocketServerListener.java(JavaWeb启动Socket操作类)
web.xml(配置文件,跟随项目启动)
断点上传(服务端)

package com.cn.csdn.seesun2012.socket;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.math.RoundingMode;
import java.net.ServerSocket;
import java.net.Socket;
import java.text.DecimalFormat;

public class FileUpLoadServer extends ServerSocket {

// 文件大小
private static DecimalFormat df = null;
// 退出标识
private boolean quit = false;

static {
// 设置数字格式,保留一位有效小数
df = new DecimalFormat("#0.0");
df.setRoundingMode(RoundingMode.HALF_UP);
df.setMinimumFractionDigits(1);
df.setMaximumFractionDigits(1);
}

public FileUpLoadServer(int report) throws IOException {
super(report);
}

/**
* 使用线程处理每个客户端传输的文件

* @throws Exception
*/
public void load() throws Exception {
System.out.println("【文件上传】服务器:" + this.getInetAddress() + " 正在运行中...");
while (!quit) {
// server尝试接收其他Socket的连接请求,server的accept方法是阻塞式的
Socket socket = this.accept();
/**
* 我们的服务端处理客户端的连接请求是同步进行的, 每次接收到来自客户端的连接请求后,
* 都要先跟当前的客户端通信完之后才能再处理下一个连接请求。 这在并发比较多的情况下会严重影响程序的性能,
* 为此,我们可以把它改为如下这种异步处理与客户端通信的方式
*/
// 收到请求,验证合法性
String ip = socket.getInetAddress().toString();
ip = ip.substring(1, ip.length());
System.out.println("服务器接收到请求,正在开启验证对方合法性IP:" + ip + "!");
// 每接收到一个Socket就建立一个新的线程来处理它
new Thread(new Task(socket, ip)).start();
}
}

/**
* 处理客户端传输过来的文件线程类
*/
class Task implements Runnable {

private Socket sk; // 当前连接
private String ips; // 当前连接IP址

public Task(Socket socket, String ip) {
this.sk = socket;
this.ips = ip;
}

public void run() {

Socket socket = sk; // 重新定义,请不要移出run()方法外部,否则连接两会被重置
String ip = ips; // 重新定义,同上IP会变
long serverLength = -1l; // 定义:存放在服务器里的文件长度,默认没有为-1
char pathChar = File.separatorChar; // 获取:系统路径分隔符
String panFu = "D:"; // 路径:存储文件盘符

DataInputStream dis = null; // 获取:客户端输出流
DataOutputStream dos = null; // 发送:向客户端输入流
FileOutputStream fos = null; // 读取:服务器本地文件流
RandomAccessFile rantmpfile = null; // 操作类:随机读取

try {
// 获取
dis = new DataInputStream(socket.getInputStream());
// 发送
dos = new DataOutputStream(socket.getOutputStream());
// 定义客户端传过来的文件名
String fileName = "";
while (fileName == "") {
// 读取客户端传来的数据
fileName = dis.readUTF();
System.out.println("服务器获取客户端文件名称:" + fileName);
File file = new File(panFu+ pathChar +"receive"+ pathChar +"" + ip + pathChar + fileName);
if (file.exists()) {
serverLength = file.length();
dos.writeLong(serverLength);
System.out.println("向客户端返回文件长度:" + serverLength + " B");
} else {
serverLength = 0l;
dos.writeLong(serverLength);
System.out.println("文件不存在");
System.out.println("向客户端返回文件长度:" + serverLength + " B");
}
}
System.out.println("服务器建立新线程处理客户端请求,对方IP:" + ip + ",传输正在进行中...");
// 从客户端获取输入流
dis = new DataInputStream(socket.getInputStream());
// 文件名和长度
long fileLength = dis.readLong();
File directory = new File(panFu + pathChar + "receive"+ pathChar +"" + ip + pathChar);
if (!directory.exists()) {
directory.mkdirs();
}
int length = 0;
byte[] bytes = new byte[1024];
File file = new File(directory.getAbsolutePath() + pathChar + fileName);
if (!file.exists()) {
// 不存在
fos = new FileOutputStream(file);
// 开始接收文件
while ((length = dis.read(bytes, 0, bytes.length)) != -1) {
fos.write(bytes, 0, length);
fos.flush();
}
} else {
// 存储在服务器中的文件长度
long fileSize = file.length(), pointSize = 0;
// 判断是否已下载完成
if (fileLength > fileSize) {
// 断点下载
pointSize = fileSize;
} else {
// 重新下载
file.delete();
file.createNewFile();
}

rantmpfile = new RandomAccessFile(file, "rw");
/*
* java.io.InputStream.skip() 用法:跳过 n 个字节(丢弃) 如果 n
* 为负,则不跳过任何字节。
*/
// dis.skip(pointSize); (已从客户端读取进度)
/**
* 资源,文件定位(游标、指针) 将ras的指针设置到8,则读写ras是从第9个字节读写到
*/
rantmpfile.seek(pointSize);

while ((length = dis.read(bytes, 0, bytes.length)) != -1) {
rantmpfile.write(bytes, 0, length);
}
}

System.out.println("======== 文件接收成功 [File Name:" + fileName + "] [ClientIP:" + ip + "] [Size:" + getFormatFileSize(file.length()) + "] ========");

} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (fos != null)
fos.close();
if (dis != null)
dis.close();
if (rantmpfile != null)
rantmpfile.close();
socket.close();
} catch (Exception e) {
e.printStackTrace();
System.out.println("Socket关闭失败!");
}
/**
* 文件传输完毕:执行后续操作(略)
*/
//DoSomeThing dst = new DoSomeThing()
//dst.save(filePath);
}
}
}

/**
* 格式化文件大小

* @param length
* @return
*/
public String getFormatFileSize(long length) {
double size = ((double) length) / (1 << 30);
if (size >= 1) {
return df.format(size) + "GB";
}
size = ((double) length) / (1 << 20);
if (size >= 1) {
return df.format(size) + "MB";
}
size = ((double) length) / (1 << 10);
if (size >= 1) {
return df.format(size) + "KB";
}
return length + "B";
}

/**
* 退出
*/
public void quit() {
this.quit = true;
try {
this.close();
} catch (IOException e) {
System.out.println("服务器关闭发生异常,原因未知");
}
}

}

断点上传(客户端)

package com.cn.csdn.seesun2012.socket;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Timer;
import java.util.TimerTask;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Client端<br><br>
* 功能说明:文件上传(断点)传输

* @author CSDN:seesun2012
* @CreateDate 2017年08月18日
* @Override 2017年11月07日
* @version 1.1
*/
public class FileUpLoadClient extends Socket{

private Logger logger = LoggerFactory.getLogger("oaLogger");

private Socket client; // Socket-客户端
private static long status = 0; // 进度条
private boolean quit = false; //退出

/**
* 构造器

* @param ip 服务端IP地址
* @param report 服务端开放的端口
* @throws UnknownHostException
* @throws IOException
*/
public FileUpLoadClient(String ip, Integer report) throws UnknownHostException, IOException {
super(ip, report);
this.client = this;
if (client.getLocalPort()>0) {
System.out.println("Cliect[port:" + client.getLocalPort() + "] 成功连接服务端");
}else{
System.out.println("服务器连接失败");
}
}

public int sendFile(String filePath) {

DataOutputStream dos = null; // 上传服务器:输出流
DataInputStream dis = null; // 获取服务器:输入流
Long serverLength = -1l; // 存储在服务器的文件长度,默认-1
FileInputStream fis = null; // 读取文件:输入流

// 获取:上传文件
File file = new File(filePath);

// ==================== 节点:文件是否存在 ==================== 
if (file.exists()) {

// 发送:文件名称、文件长度
try {
dos = new DataOutputStream(client.getOutputStream());
} catch (IOException e2) {
logger.error("Socket客户端:1.读取输出流发生错误");
e2.printStackTrace();
}
try {
dos.writeUTF(file.getName());
dos.flush();
dos.writeLong(file.length());
dos.flush();
} catch (IOException e2) {
logger.error("Socket客户端:2.向服务器发送文件名、长度发生错误");
e2.printStackTrace();
}

// 获取:已上传文件长度
try {
dis = new DataInputStream(client.getInputStream());
} catch (IOException e2) {
logger.error("Socket客户端:3.向服务器发送文件名、长度发生错误");
e2.printStackTrace();
}
while(serverLength==-1){
try {
serverLength = dis.readLong();
} catch (IOException e) {
logger.error("Socket客户端:4.读取服务端长度发送错误");
e.printStackTrace();
}
}

// 读取:需要上传的文件
try {
fis = new FileInputStream(file);
} catch (FileNotFoundException e2) {
logger.error("Socket客户端:5.读取本地需要上传的文件失败,请确认文件是否存在");
e2.printStackTrace();
}
// 发送:向服务器传输输入流
try {
dos = new DataOutputStream(client.getOutputStream());
} catch (IOException e2) {
logger.error("Socket客户端:6.向服务器传输输入流发生错误");
e2.printStackTrace();
}

System.out.println("======== 开始传输文件 ========");
byte[] bytes = new byte[1024];
int length = 1024;
long progress = serverLength;

// 设置游标:文件读取的位置
if (serverLength==-1l) {
serverLength = 0l;
}
try {
fis.skip(serverLength);
} catch (IOException e1) {
logger.error("Socket客户端:7.设置游标位置发生错误,请确认文件流是否被篡改");
e1.printStackTrace();
}

try {
while (((length = fis.read(bytes, 0, bytes.length)) != -1) && quit != true) {
dos.write(bytes, 0, length);
dos.flush();
progress += length;
status = (100 * progress / file.length());
}
} catch (IOException e) {
logger.error("Socket客户端:8.设置游标位置发生错误,请确认文件流是否被篡改");
e.printStackTrace();
}finally {
if (fis != null)
try {
fis.close();
} catch (IOException e1) {
logger.error("Socket客户端:9.关闭读取的输入流异常");
e1.printStackTrace();
}
if (dos != null)
try {
dos.close();
} catch (IOException e1) {
logger.error("Socket客户端:10.关闭发送的输出流异常");
e1.printStackTrace();
}
try {
client.close();
} catch (IOException e) {
logger.error("Socket客户端:11.关闭客户端异常");
e.printStackTrace();
}
}
System.out.println("======== 文件传输成功 ========");

}else{
logger.error("Socket客户端:0.文件不存在");
return -1;
}

return 1;
}

/**
* 进度条
*/
public void statusInfo(){
Timer time = new Timer();
time.schedule(new TimerTask() {
long num = 0;
@Override
public void run() {
if (status>num) {
System.out.println("当前进度为:"+status+"%");
num = status;
}
if (status==101) {
System.gc();
}
}
},0,100);
}

/**
* 退出
*/
public void quit() {
this.quit = true;
try {
this.close();
} catch (IOException e) {
System.out.println("服务器关闭发生异常,原因未知");
}
}

}

断点上传(参数设置)

package com.cn.csdn.seesun2012.socket;

public interface FinalVariables {
// 服务端IP
public final static String SERVER_IP = "192.168.1.10010"; 
// 服务端端口
public final static int SERVER_PORT = 10086; 
// 开启配置
public final static String IS_START_SERVER = "instart";

}

断点上传(JavaWeb启动服务端)

package com.cn.csdn.seesun2012.socket;

import java.util.Timer;
import java.util.TimerTask;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

/**
* Server端<br><br>
* 功能说明:服务端监听开启Servlet

* @author CSDN:seesun2012
* @CreateDate 2017年08月18日
* @Override 2017年11月07日
* @Override 2017年11月14日
* @version 1.3
*/
public class SocketServerListener extends HttpServlet{

private static final long serialVersionUID = -999999999999999999L;

// 初始化启动Socket服务
@Override
public void init() throws ServletException {
super.init();
for(int i = 0; i < 3; i++){
if ("instart".equals(FinalVariables.IS_START_SERVER )) {
open();
break;
}
}
}

public void open(){
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@SuppressWarnings("resource")
@Override
public void run() {
try {
FileUpLoadServer fileUpLoadServer = new FileUpLoadServer(FinalVariables.SERVER_PORT);
fileUpLoadServer.load();
} catch (Exception e) {
e.printStackTrace();
}
}
}, 3000);
}
}

web.xml配置(跟随项目启动)

<?xml version="1.0" encoding="UTF-8"?>

<web-app>

<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

<servlet>
<servlet-name>SocketServerListener</servlet-name>
<servlet-class>com.cn.csdn.seesun2012.socket.SocketServerListener</servlet-class>
<load-on-startup>10</load-on-startup>
</servlet>

<display-name>seesun2012</display-name>

</web-app>

功能展示截图:

在页面中选择好相应的上传目录,点击粘贴上传按钮,数据即可快速开始上传,电脑重启后打开网页也可以按当前进度继续上传

文件和目录下载

批量下载 同时选择多个需要下载的文件 然后点击下载按钮,设置下载目录文件夹

Mac控件安装教程与演示说明:

http://t.cn/AijgiFgW

http://t.cn/Aijg6z08

Linux控件安装教程与演示说明:

http://t.cn/Aijg6Lv3

http://t.cn/Aijg64k6

控件包下载:

MacOS:http://t.cn/Aijg65dZ

Linux:http://t.cn/Aijg6fRV

cab(x86):http://t.cn/Ai9pmG8S

cab(x64):http://t.cn/Ai9pm04B

xpi:http://t.cn/Ai9pubUc

crx:http://t.cn/Ai9pmrcy

exe:http://t.cn/Ai9puobe

示例下载:

jsp-eclipse:http://t.cn/Ai9p3LSx

jsp-myeclipse:http://t.cn/Ai9p3IdC

在线教程:

jsp-文件管理器教程:http://t.cn/AiNhmilv

个人博客:http://t.cn/AiY7heL0

www.webuploader.net

java+下载+大文件断点续传的更多相关文章

  1. java解决大文件断点续传

    第一点:Java代码实现文件上传 FormFile file = manform.getFile(); String newfileName = null; String newpathname =  ...

  2. java之大文件断点续传

    针对某些场景下,面对服务文件大,或者服务端服务器不稳定时使用该模块.功能代码如下: import java.io.File; import java.io.FileOutputStream; impo ...

  3. java http大文件断点续传上传

    因为需要研究下断点上传的问题.找了很久终于找到一个比较好的项目. 效果: 上传中,显示进度,时间,百分比. 点击[Pause]暂停,点击[Resume]继续. 2,代码分析 项目进行了封装使用最简单的 ...

  4. iOS开发之网络编程--使用NSURLConnection实现大文件断点续传下载+使用输出流代替文件句柄

    前言:本篇讲解,在前篇iOS开发之网络编程--使用NSURLConnection实现大文件断点续传下载的基础上,使用输出流代替文件句柄实现大文件断点续传.    在实际开发中,输入输出流用的比较少,但 ...

  5. java+大文件断点续传

    用JAVA实现大文件上传及显示进度信息 ---解析HTTP MultiPart协议 (本文提供全部源码下载,请访问 https://github.com/1269085759/up6-jsp-mysq ...

  6. php实现大文件断点续传下载实例

    php实现大文件断点续传下载实例,看完你就知道超过100M以上的大文件如何断点传输了,这个功能还是比较经典实用的,毕竟大文件上传功能经常用得到. require_once('download.clas ...

  7. java+web+大文件上传下载

    文件上传是最古老的互联网操作之一,20多年来几乎没有怎么变化,还是操作麻烦.缺乏交互.用户体验差. 一.前端代码 英国程序员Remy Sharp总结了这些新的接口 ,本文在他的基础之上,讨论在前端采用 ...

  8. 【原创】用JAVA实现大文件上传及显示进度信息

    用JAVA实现大文件上传及显示进度信息 ---解析HTTP MultiPart协议 (本文提供全部源码下载,请访问 https://github.com/grayprince/UploadBigFil ...

  9. HTML5 大文件断点续传完整思路整理

    需求: 支持大文件批量上传(20G)和下载,同时需要保证上传期间用户电脑不出现卡死等体验: 内网百兆网络上传速度为12MB/S 服务器内存占用低 支持文件夹上传,文件夹中的文件数量达到1万个以上,且包 ...

随机推荐

  1. PowerBuilder学习笔记之打开Expressino属性页

  2. High load average analyze

    https://www.tummy.com/articles/isolating-heavy-load/ https://www.tecmint.com/understand-linux-load-a ...

  3. netcat瑞士军刀实现电脑远程控制termux

    关于nc实现远程控制termux 1.首先termux安装namp pkg install namp 2.windows系统安装netcat 此为netcat下载连接 下载得到zip压缩包,解压得到里 ...

  4. Verilog HDL

    https://wenku.baidu.com/view/9943b7acf524ccbff1218463.html https://hdlbits.01xz.net/wiki/Main_Page h ...

  5. 回忆一下Node(随时更改,想到什么写什么)

    什么是Node? Node.js 是一个基于Chrome V8 引擎的JavaScript运行环境 Node.js使用了一个事件驱动.非阻塞式I/O的模型,使其轻量又高效 事件驱动: 任务执行,发布者 ...

  6. linux 下调用wps 注意

    记录笔记以防忘记 wps 是界面程序,linux 必须在界面终端中调用 wps 命令才能打开软件 xshell 连接时启动tomcat ,wps命令 会使用Xshell 的隧道转发,只有在界面下的终端 ...

  7. Elasticsearch vs Solr 搜索引擎对比和选型

    前言 全文搜索属于最常见的需求,开源的 Elasticsearch 是目前全文搜索引擎的首选. 基于Lucene它可以快速地储存.搜索和分析海量数据.维基百科.Stack Overflow.Githu ...

  8. JS知识体系【JQ】附加理论+视频地址铺助学习

    理论部分:https://www.jianshu.com/p/e10792076c6e  //不吃鱼的猫_8e95---简书平台 https://www.cnblogs.com/hongqin/p/5 ...

  9. Windows Server 2016 IIS的安装与配置

    1. 打开服务器管理器,点击[添加角色和功能选项].        2. 进入“添加角色和功能向导”页面,点击下一步. 3. 安装类型选择[基于角色或基于功能的安装],点击下一步. 4. 进入服务器选 ...

  10. Loadrunner 计算保留两位小数不四舍五入

    有时候在测试过程中会截取返回值,当你截取的值不是最终的值,需要进行计算后才能使用并且需要保留两位小数,不进行四舍五入的计算: 此时 我使用了各种办法,但是最终我采用了一种最直接,最暴力的方法就是先乘后 ...