Отправка и получение файлов на сокете
Я отправляю файлы на удаленный Android-клиент с сервера Java. Я пишу байты, используя outputtream. При чтении этих байтов метод read () продолжает пытаться читать байты после завершения потока. если я закрою выходной поток на стороне сервера, штраф за работу операции чтения. Но я должен снова записать файл в тот же сокет, так что не могу закрыть выходной поток, какое-либо решение?
ПРИМЕЧАНИЕ: МОЙ КОД РАБОТАЕТ ХОРОШО ДЛЯ РАЗДЕЛЕНИЯ ОДНОГО ФАЙЛА
КОД ДЛЯ ЗАПИСИ ФАЙЛА
public static void writefile(String IP, String filepath, int port, OutputStream out ) throws IOException {
ByteFileConversion bfc = new ByteFileConversion();
byte[] file = bfc.FileToByteConversion(filepath);
out.write(file, 0, file.length);
out.close(); // i donot want to close this and how can I tell reading side that stream is ended.
System.out.println("WRITTEN");
}
Здесь я читаю файл на Android:
public Bitmap fileReceived(InputStream is)
{
Bitmap bitmap = null;
String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath();
String fileName = "a.png";
String imageInSD = baseDir + File.separator + fileName;
// System.out.println(imageInSD);
if (is!= null) {
FileOutputStream fos = null;
OutputStream bos = null;
try {
bos = new FileOutputStream(imageInSD);
byte[] aByte = new byte[1024];
int bytesRead;
int index = 0;
DataInputStream dis = new DataInputStream(is);
while ( (bytesRead = is.read(aByte)) >0 ) {
index = bytesRead +index;
bos.write(aByte, 0, bytesRead);
// index = index+ bytesRead;
System.out.println("Loop"+aByte+ " byte read are "+bytesRead+ "whree index ="+ index);
}
bos.flush();
bos.close();
Log.i("IMSERVICE", "out of loop");
java.io.FileInputStream in = new FileInputStream(imageInSD);
bitmap = BitmapFactory.decodeStream(in);
bitmap = BitmapFactory.decodeFile(imageInSD);
Log.i("IMSERVICE", "saved");
// if (bitmap != null)
// System.out.println("bitmap is "+ bitmap.toString());
} catch (IOException ex) {
// Do exception handling
// Log.i("IMSERVICE", "exception ");
System.out.println("ex");
}
}
return bitmap;
}
На самом деле я хочу сбросить подключение сокета
Заранее спасибо