guys ...what happened ....no reply......ok i am having another problem regarding my project...i am tryomg to send a file from my PC to my mobile phone.wen i run the following code,a message is displayed on my phone that "ACCEPT CONNECTION FROM [PCNAME]".but wen i accept the connection ,it gets disconnected...the two files are.........
send.java file
import java.io.*;
import java.io.File;
import java.io.InputStream;
import java.io.FileInputStream;
import java.io.IOException;
public class send extends
Thread
{
String connection;
public byte[] getBytesFromFile(String file) throws IOException {
InputStream is = new FileInputStream(file);
System.out.println("\nDEBUG: FileInputStream is " + file);
// Get the size of the file
long length = file.length();
System.out.println("DEBUG: Length of " + file + " is " + length + "\n");
if (length > Integer.MAX_VALUE) {
System.out.println("File is too large to process");
return null;
}
// Create the byte array to hold the data
byte[] bytes = new byte[(int)length];
// Read in the bytes
int offset = 0;
int numRead = 0;
while ( (offset < bytes.length)
&&
( (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) ) {
offset += numRead;
}
is.close();
return bytes;
}
public static void main(String args[])
{
try{
send obj=new send();
byte[] filebytes=obj.getBytesFromFile("C:/Users/Sarbjit/Pictures/21.jpg");
Thread t=new Thread(new SendFileTask(filebytes,"abc"));
t.start();
}
catch(Exception e){}
}}
this is another file....
SendFileTask.java
import java.io.OutputStream;
import javax.microedition.io.Connection;
import javax.microedition.io.Connector;
import javax.obex.ClientSession;
import javax.obex.HeaderSet;
import javax.obex.Operation;
public class SendFileTask implements Runnable {
private String url;
private byte[] file;
private String filename;
public SendFileTask(byte[] file, String filename) {
this.file=file;
this.filename=filename;
}
public void run(){
try {
Connection connection =
Connector.open("btspp://0018427450CD:1;authenticate=false;encrypt=false;master=false");
// connection obtained
System.out.println(connection);
// now, let’s create a session
// and a headerset objects
ClientSession cs =
(ClientSession) connection;
HeaderSet hs = cs.createHeaderSet();
// now let’s send the connect header
cs.connect(hs);
hs.setHeader(HeaderSet.NAME, filename);
// content-type could be
// checked from a filename
hs.setHeader(HeaderSet.TYPE, "image/jpeg");
hs.setHeader(
HeaderSet.LENGTH,
new Long(file.length));
Operation putOperation = cs.put(hs);
OutputStream outputStream =
putOperation.openOutputStream();
outputStream.write(file);
// file push complete
outputStream.close();
putOperation.close();
cs.disconnect(null);
connection.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}