问一个socket传输文件的问题 要利用socket向客户端传容量比较大的文件,比如大的图片,大的视频文件等,有什么好的优化方案吗?把文件切割成小片的话,怎么切割,有具体的实例吗?谢谢大家帮助。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 文件切割的程序:http://www.99inf.net/SoftwareDev/Java/54606.htm利用多线程并发读写。效率可能会快一点。 我也来个分割文件的程序,供参考,合并的未贴:public class FileDivision { // 单个文件大小 private static final long SPLIT_SIZE = 5 * 1024 * 1024; /** *分割文件 * * @author Jason,Wang * @param filePath * 要分割的文件路径 * @return int 分割的数量 */ public static int split(String filePath) { // 子文件数量 int number = 1; // 文件通道 FileChannel in = null; try { in = new FileInputStream(filePath).getChannel(); // 文件长度 long fileLength = in.size(); long position = 0; while (position < fileLength) { // 子文件 FileChannel out = new FileOutputStream(filePath + (number++)) .getChannel(); in.transferTo(position, SPLIT_SIZE, out); out.close(); position += SPLIT_SIZE; } } catch (Exception e) { e.printStackTrace(); number = 1; } finally { if (in != null) try { in.close(); } catch (IOException e) { e.printStackTrace(); } } return number; }} http://www.99inf.net/SoftwareDev/Java/54606.htm利用多线程技术也能够提高效率数据已经读入内存中,因为要对这些数据进行一下处理,然后再传图片、视频这个文件分割技术的代码也许会对你有些帮助public class FileDivision { // 单个文件大小 private static final long SPLIT_SIZE = 5 * 1024 * 1024; /** *分割文件 * * @author Jason,Wang * @param filePath * 要分割的文件路径 * @return int 分割的数量 */ public static int split(String filePath) { // 子文件数量 int number = 1; // 文件通道 FileChannel in = null; try { in = new FileInputStream(filePath).getChannel(); // 文件长度 long fileLength = in.size(); long position = 0; while (position < fileLength) { // 子文件 FileChannel out = new FileOutputStream(filePath + (number++)) .getChannel(); in.transferTo(position, SPLIT_SIZE, out); out.close(); position += SPLIT_SIZE; } } catch (Exception e) { e.printStackTrace(); number = 1; } finally { if (in != null) try { in.close(); } catch (IOException e) { e.printStackTrace(); } } return number; } } 4楼的推荐文章应该提够一点思路,改改应该能够运用到socket上来。1.分割文件2.分多个线程,每个线程传输一个分割快到客户端3.客户端等所有接受线程都完成的时候在合并文件 JRootPane这个类是不是多余的啊? java问题,高手来下 java 操作符问题 急啊,大家帮帮忙 新建SWT/Jface 群 18687738 JDBC连接sql server 500分寻求一递归算法(急):测试成功后马上给分 如何使用jsdk类库中的接口? about the Object.clone() ?? 有关数据库连接池的问题 关于日期的处理 请教关于 SQL 查询唯一值 语句
// 单个文件大小
private static final long SPLIT_SIZE = 5 * 1024 * 1024; /**
*分割文件
*
* @author Jason,Wang
* @param filePath
* 要分割的文件路径
* @return int 分割的数量
*/
public static int split(String filePath) {
// 子文件数量
int number = 1;
// 文件通道
FileChannel in = null;
try {
in = new FileInputStream(filePath).getChannel();
// 文件长度
long fileLength = in.size();
long position = 0;
while (position < fileLength) {
// 子文件
FileChannel out = new FileOutputStream(filePath + (number++))
.getChannel();
in.transferTo(position, SPLIT_SIZE, out);
out.close();
position += SPLIT_SIZE;
}
} catch (Exception e) {
e.printStackTrace();
number = 1;
} finally {
if (in != null)
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return number;
}
}
利用多线程技术也能够提高效率
数据已经读入内存中,因为要对这些数据进行一下处理,然后再传图片、视频
这个文件分割技术的代码也许会对你有些帮助
public class FileDivision { // 单个文件大小
private static final long SPLIT_SIZE = 5 * 1024 * 1024;
/** *分割文件 * * @author Jason,Wang * @param filePath * 要分割的文件路径 * @return int 分割的数量 */
public static int split(String filePath)
{ // 子文件数量 int number = 1;
// 文件通道 FileChannel in = null;
try { in = new FileInputStream(filePath).getChannel();
// 文件长度 long fileLength = in.size();
long position = 0; while (position < fileLength)
{ // 子文件 FileChannel out = new FileOutputStream(filePath + (number++)) .getChannel();
in.transferTo(position, SPLIT_SIZE, out); out.close(); position += SPLIT_SIZE; } }
catch (Exception e) { e.printStackTrace(); number = 1; }
finally { if (in != null) try { in.close(); } catch (IOException e) { e.printStackTrace(); } } return number; } }
1.分割文件
2.分多个线程,每个线程传输一个分割快到客户端
3.客户端等所有接受线程都完成的时候在合并文件