9
我正在编写从客户端上传文件到我的服务器的代码,性能没有我认为的那么快。使用Java传输文件的最佳方式是什么?
我有当前的代码片段正在进行文件传输,我想知道如何加快传输速度。
对不起所有的代码:
InputStream fileItemInputStream ;
OutputStream saveFileStream;
int[] buffer;
while (fileItemInputStream.available() > 0) {
buffer = Util.getBytesFromStream(fileItemInputStream);
Util.writeIntArrToStream(saveFileStream, buffer);
}
saveFileStream.close();
fileItemInputStream.close();
使用util方法如下:
public static int[] getBytesFromStream(InputStream in, int size) throws IOException {
int[] b = new int[size];
int count = 0;
while (count < size) {
b[count++] = in.read();
}
return b;
}
和:
public static void writeIntArrToStream(OutputStream out, int[] arrToWrite) throws IOException {
for (int i = 0; i < arrToWrite.length; i++) {
out.write(arrToWrite[i]);
}
}
我不必担心文件中的无符号字节溢出Java签名的字节? – jjnguy 2008-11-10 07:38:44