aspose file tools*
The moose likes Android and the fly likes transfer large file using java code in android Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Mobile » Android
Bookmark "transfer large file using java code in android" Watch "transfer large file using java code in android" New topic
Author

transfer large file using java code in android

akash priyadarshi
Greenhorn

Joined: May 18, 2012
Posts: 6
I have written java code using socket programming to transfer files between android(aakash tablet) it works fine with files of small size . but for large file it shows :
java.net.SocketException: Connection reset.

Please suggest any change to so, that i can even transfer large file (like introducing buffer(but i do not know how to use that ))

here is code for sending file :


package javaapplication1;

import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.io.File ;
/**
*
* @author akash
*/
public class Transfer {
public static void fileTransfer(Socket client, File file ,DataOutputStream out) {
try {


System.out.println(file.length());
String length= ""+file.length();
FileInputStream fileInputStream = new FileInputStream(file);
OutputStream socketOutputStream = client.getOutputStream();
long startTime = System.currentTimeMillis();

byte buffer[]= new byte[64] ;
int read;
int readTotal = 0;
out.writeUTF(length);

System.out.println(file.length());
for(int j =(int)file.length();j>0;j-=64) {
read = fileInputStream.read(buffer);
socketOutputStream.write(buffer, 0, read);
readTotal += read;
}


long endTime = System.currentTimeMillis();
System.out.println(readTotal + " bytes written in " + (endTime - startTime) + " ms.");
Thread.sleep(10000);

//socketOutputStream.close();
fileInputStream.close();
} catch (Exception e) {
System.out.println(e);
}
}
}


here is code for receiving the file :

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.io.*;

public class Accept {

public void FileAccept(InputStream clientInputStream, String fileName,
DataInputStream in) {

FileOutputStream file;
boolean flag = true;
System.out.print("hi file transfer start in few sec...");

try {
long startTime = System.currentTimeMillis();

file = new FileOutputStream(fileName, true);
System.out.println("file trsnsfer start of file");
String s = in.readUTF();
byte[] buffer = new byte[64];
int read;
int totalRead = 0;

int i = Integer.parseInt(s);
System.out.println(i + " " + s);
for (int j = i; j > 0; j -= 64) {

{
if(j<64){buffer=new byte[j];}
read = clientInputStream.read(buffer);
file.write(buffer);
}
totalRead += read;
}
System.out.println(fileName);
file.flush();
file.close();
long endTime = System.currentTimeMillis();
System.out.println(totalRead + " bytes read in "
+ (endTime - startTime) + " ms.");
} catch (FileNotFoundException f) {
System.out.println("error two " + f);
} catch (IOException e) {
System.out.println(e);
System.exit(0);
}

}
}

thanks in advance.

Przemek Boryka
Ranch Hand

Joined: Dec 06, 2011
Posts: 51

hi

Try to create your own protocol for example let the data size is 1024 bytes (so your buffer will be : 1024 bytes data size + packet header). Divide your file to packet size and sent them progressively.

Simple communication between 2 devices
ANDROID OTHER DEVICE
<----- REQUEST FILE

SEND INFO ABOUT FILE ---->

-------------------------------------------------------------------- do in loop
<----- REQUEST FIRS AND NEXT PACKETS

SEND FIRST PACKET AND NEXT PACKETS --->

<---- CONFIRM RECEIVE PACKET

PREPARE NEXT PACKET
-------------------------------------------------------------------- end loop


I will resolve very similar problem in my project. I need to send JPG to other none Android device (without operation system), but I have already have implemented our communication protocol, so I think it will be easy to do it for me . The device for the other side is equipment in Bluetooth module and it works on Atmel processor. ;)

Regards Przemek
akash priyadarshi
Greenhorn

Joined: May 18, 2012
Posts: 6
thanks .

I tried to create my protocol , but it did not worked . But by adjusting buffer size i make it working now.
thomas brian
Greenhorn

Joined: Apr 19, 2012
Posts: 16
Try out the following code:
Here's an example of copying one stream to another that doesn't really care about the size of the data being streamed:

public static void stream(InputStream in, OutputStream out)
throws IOException {
byte[] buf = new byte[1024];
int bytesRead = 0;

try {

while (-1 != (bytesRead = in.read(buf, 0, buf.length))) {
out.write(buf, 0, bytesRead);
}

} catch (IOException e) {
log.error("Error with streaming op: " + e.getMessage());
throw (e);
} finally {
try{
in.close();
out.flush();
out.close();
} catch (Exception e){}//Ignore
}
}
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: transfer large file using java code in android