Thanx Graham! with your help, my code works! The file is read from the internal storage and is uploaded to the webserver thru a ASP.NET .aspx page.
This is the final code to read the file from the internal storage:
Code:
public void EnviarFichero(String iPath) throws IOException{
String path =iPath;
Image img = null;
String sURL = "http://webserver/virtualfolder/upload.aspx";
HttpConnection httpConnection = null;
DataOutputStream os = null;
String mensaje="";
//img = Image.createImage(path);
httpConnection = (HttpConnection) Connector.open(sURL, Connector.READ_WRITE, false);
httpConnection.setRequestProperty("Connection", "keep-alive");
httpConnection.setRequestMethod(HttpConnection.POST);
String photos = "fileconn.dir.photos";
path = System.getProperty(photos) + iPath;
try {
FileConnection fc = (FileConnection)Connector.open(path, Connector.READ);
if(!fc.exists()) {
System.out.println("Archivo no existe!");
}
else {
int size = (int)fc.fileSize();
InputStream is = fc.openInputStream();
byte bytesFichero[] = new byte[size];
is.read(bytesFichero, 0, size);
httpConnection.setRequestProperty("Content-Type", "multipart/form-data");
httpConnection.setRequestProperty("Content-Length", bytesFichero.length + "");
os = httpConnection.openDataOutputStream();
// enviar el archivo
int index = 0;
int sizeMaximo = 1024;
do{
System.out.println("write:" + index);
if((index+sizeMaximo)>bytesFichero.length){
sizeMaximo = bytesFichero.length - index;
}
os.write(bytesFichero, index, sizeMaximo);
index+=sizeMaximo;
}while(index<bytesFichero.length);
os.flush();
mensaje=iPath + " enviado.";
}
} catch (IOException ioe) {
mensaje="IOException: "+ioe.getMessage();
} catch (IllegalArgumentException iae) {
mensaje="IllegalArgumentException: "+iae.getMessage();
}
Alert Alerta1 = new Alert("Status", mensaje, null, AlertType.INFO);
pantalla=Display.getDisplay(this);
pantalla.setCurrent(Alerta1);
}//fin de public void EnviarFichero
This is the code in the ASP.NET page "upload.aspx"
Code:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Introducir aquí el código de usuario para inicializar la página
If Not Page.IsPostBack Then
Dim str As System.IO.Stream, strmContents As String
Dim counter, strLen, strRead As Integer
str = Request.InputStream ' Create a Stream object.strArr
strLen = CInt(str.Length) ' Find number of bytes in stream.
Dim strArr(strLen) As Byte ' Create a byte array.
strRead = str.Read(strArr, 0, strLen) ' Read stream into byte array.
' Create a file and write the byte data to a file.
Dim oFileStream As System.IO.FileStream
oFileStream = New System.IO.FileStream(Server.MapPath("_tmm/" & Date.Now.Ticks.ToString), System.IO.FileMode.Create)
oFileStream.Write(strArr, 0, strArr.Length)
oFileStream.Close()
End If
End Sub
I have two final questions:
1) Is there a way to put the real name to the uploaded file? that is, in the j2me code, i sent the bytes, not the name. I had to assign a name to the uploaded file, based upon the Ticks property of the Date class (in ASP.NET ....) but i would like to assign the real name "as is" in the mobile internal storage.
2) Is there a way to initialize the FileBrowser "control" to start in a predefined folder, in order to avoid the navigation thru the filesystem of the device ( every time i navigate thru the folder it prompts me the security restrictions and i hope you agree with me: IS ANNOYING ....!!! =0) - or at least just one question...?
Thanx in advance and very thanx again for your kind help!