我的客户端服务器配置需要执行以下操作:
我知道如何传输文件并进行转换,因此前两步已完成。但后来我需要发回视频。天真的方法,这将使服务器看起来像这样:
//Receive file
File copy = new File("copypath");
byte[] bytearray = new byte[10000000];
InputStream is = socket.getInputStream();
FileOutputStream fos = new FileOutputStream(copy);
BufferedOutputStream bos = new BufferedOutputStream(fos);
int bytesRead = is.read(bytearray, 0, bytearray.length);
int currentTot = bytesRead;
do {
bytesRead = is.read(bytearray, currentTot,
(bytearray.length - currentTot));
if (bytesRead >= 0)
currentTot += bytesRead;
} while (bytesRead > -1);
bos.write(bytearray, 0, currentTot);
//Convert file
File converted = convert(copy);
//Send file back:
bytearray = new byte[(int) converted.length()];
FileInputStream fin = new FileInputStream(converted);
BufferedInputStream bin = new BufferedInputStream(fin);
bin.read(bytearray, 0, bytearray.length);
OutputStream os = socket.getOutputStream();
os.write(bytearray, 0, bytearray.length);
客户端代码为:
OutputStream os = socket.getOutputStream();
//System.out.println("Sending file...");
os.write(bytearray, 0, bytearray.length);
os.flush();
// System.out.println("File transfer complete");
//Y recibe
File copy = new File(convertedName);
bytearray = new byte[10000000];
InputStream is = socket.getInputStream();
FileOutputStream fos = new FileOutputStream(copy);
BufferedOutputStream bos = new BufferedOutputStream(fos);
int bytesRead = is.read(bytearray,0,bytearray.length);
int currentTot = bytesRead;
do{
bytesRead =
is.read(bytearray,currentTot,(bytearray.length-currentTot));
if(bytesRead >= 0)currentTot+=bytesRead;
} while(bytesRead > -1);
bos.write(bytearray,0,currentTot);
......没有用。关于如何进行的任何提示?