socket 传输文件慢

lovegjj 2009-11-30 10:34:59
我的工程部署在AIX系统的WebSphere(WAS)下,但socket传输很慢,我跟我同事两台笔记本之间传输还是可以的,但我们是局域网。以下是我的代码,希望高手能帮我看下!谢谢了!
------------发送文件方法---------------------------
public boolean sendFile(String fileName) {
try {
File file = new File(fileName);
if (!file.exists()) {
runLog("文件不存在!");
return false;
}
long fileSize = file.length();
String str = Constant.FILE_RECIEVE + ":" + file.getName() + ":"
+ Long.toString(fileSize) + ":" + transBufferSize ;
out.println(str);
str = in.readLine();
FileInputStream fileInputStream = new FileInputStream(file);
// 分块传输
boolean transFlag = true;
int off = 0;
while (transFlag) {
int bufSize = transBufferSize;
if (fileSize < transBufferSize) {
bufSize = Integer.parseInt(Long.toString(fileSize));
transFlag = false;
}
byte[] buf = new byte[bufSize];
fis.read(buf);
outStream.write(buf);
fileSize = fileSize - transBufferSize;
off += transBufferSize;
}

outStream.flush();
fileInputStream.close();
String result = "";
// 从服务端接收返回的消息
result = this.recieveMessage();
if (!Constant.SUCCESS.equals(result)) {
return false;
}
return true;
} catch (IOException e) {
return false;
}
}
-------------------------------------------------------
----------------------接收文件方法---------------------
private boolean recieveFile(String rootPath, String str) {
log.debug("-->SUNITS-->SocketServerService-->recieveFile-->");
// 分块接收,客户端传输过来的文件信息
String[] content = str.split(":");
boolean flag = true;
boolean returnResult =true;
String size = content[2].trim();
// 得到文件大小
long fileSize = Long.parseLong(size);
long tempSize = fileSize;

// 分段传输块大小,即发送的时候读取文件的大小,以字节为单位
transBufferSize = Integer.parseInt(content[3].trim());
FileOutputStream outputStream = null;
try {
File fileFolder = new File(rootPath);
if (!fileFolder.exists()) {
fileFolder.mkdirs();
}
File file = new File(rootPath + File.separator + content[1].trim());

outputStream = new FileOutputStream(file);
while (flag) {
int bufSize = transBufferSize;
if (fileSize < transBufferSize) {
bufSize = Integer.parseInt(Long.toString(fileSize));
flag = false;
}
byte[] buf = new byte[bufSize];
dis.readFully(buf);
outputStream.write(buf);
fileSize -= bufSize;
}


} catch (IOException e) {
e.printStackTrace();
returnResult = false;

} finally {
try {
outputStream.flush();
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
log.error("--SUNITS-->SOcketTransServer-->recieveFile-->关闭流IOException异常-->"+e.toString());
returnResult = false;
}

}
return returnResult;
}
----------------------------------------------------
...全文
210 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
「已注销」 2012-05-14
  • 打赏
  • 举报
回复
我遇到类似问题,两份产不多的代码,速度相差很多,楼主解决木有啊?
QQ:294940615
crazylaa 2009-12-02
  • 打赏
  • 举报
回复
不懂帮顶
studying_ 2009-12-01
  • 打赏
  • 举报
回复
给楼主顶起来先,希望你的问题早日被解决撒。
我是一道光_ 2009-12-01
  • 打赏
  • 举报
回复
哈哈,我可是特地跑过来接分的哈。


jf 我散的可是100分呢。。。 哈哈
lovegjj 2009-12-01
  • 打赏
  • 举报
回复
我想问下ArrayBlockingQueue使用了这个类,会不会很占内存?
lovegjj 2009-11-30
  • 打赏
  • 举报
回复
每次传输大小为transbuffersize=65536

51,410

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧