Uploading a file from a J2ME application

Ok, obviously this is either not interesting, or no one really knows what to do, I would love any ideas even if they dont work, just anything that would help would be great :)

I am extremely new to servlet programming and reasonably new to J2ME. I want to upload a file from my device to a central server. I am not sure how to go about starting this. I have a html form that requests a file and posts it to the servlet fine, but I am not sure how I would get my J2ME application to do the same. Do I replicate the html from with in the program? Any help would be much appreciated.

My basic servlet and Java code is:

Java

public String sendPostRequest(String urlstring)throws IOException{

HttpConnection hc =null;

DataInputStream dis =null;

DataOutputStream dos =null;

String message ="";

// the request body

FileSystem fs =new FileSystem();

byte[] file = fs.returnFile("c:/other/dot/helloworld.txt");

if(file !=null){

String fileString = ("file=" +new String(file));

try{

// an HttpConnection with both read and write access

hc = (HttpConnection)Connector.open(urlstring, Connector.READ_WRITE);

// set the request method to POST

hc.setRequestMethod(HttpConnection.POST);

// set request type

// Could possibly need to be "application/x-www-form-urlencoded"?

hc.setRequestProperty("Content-Type","multipart/form-data");

hc.setRequestProperty("Content-Length", fileString.length()+"" );

// obtain DataOutputStream for sending the request string

dos = hc.openDataOutputStream();

byte[] requestBody = fileString.getBytes();

// send request string to Web server

for (int i = 0; i < requestBody.length; i++){

dos.writeByte(requestBody[i]);

}

// flush it out

dos.flush();

// obtain DataInputStream for receiving server responses

dis =new DataInputStream(hc.openInputStream());

// retrieve the responses from Web server

int ch;

while ((ch = dis.read()) != -1){

message = message + (char) ch;

}

}finally{

// free up i/o streams and http connection

if (hc !=null)hc.close();

if (dis !=null)dis.close();

if (dos !=null)dos.close();

}

}

Servlet

import java.io.*;

import java.util.*;

import javax.servlet.*;

import javax.servlet.http.*;

import com.oreilly.servlet.MultipartRequest;

publicclass DNTUploadextends HttpServlet{

publicvoid doPost(HttpServletRequest req, HttpServletResponse res)throws ServletException, IOException{

res.setContentType("text/html");

PrintWriter out = res.getWriter();

try{

MultipartRequest multi =new MultipartRequest(req,"C:\\Servlets+JSP\\DNT", 5*1024*1024);

Enumeration files = multi.getFileNames();

String name = (String)files.nextElement();

File f = multi.getFile(name);

if(f !=null){

out.println("<HTML>");

out.println("<HEAD><TITLE>FileUploader</TITLE></HEAD>");

out.println("<BODY>");

out.println("<H1>Should have saved file</H1>");

out.println("</BODY>");

out.println("</HTML>");

}

else{

res.setHeader("valserror","ERROR");

}

}catch(Exception e){

out.println("<HTML>");

out.println("<BODY>");

out.println("failure!!");

out.println("</BODY>");

out.println("</HTML>");

}

}

}

Message was edited by:

-fluffy-

[6531 byte] By [-fluffy-a] at [2007-11-27 9:32:44]
# 1

I've only done one small j2me program and not recently, but here is a link that can give you some of the basics: http://today.java.net/pub/a/today/2005/02/09/j2me1.html

I'm guessing, but I believe you have to use http sockets to accomplish a file transfer with a phone. Also, there are some limitations with full sockets depending on which type of phone you using to write your app to.

Hope that helps!

jamesEstona at 2007-7-12 22:51:45 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...