Here is the code that can download file from server in j2me
import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import javax.microedition.io.Connector; import javax.microedition.io.HttpConnection; import javax.microedition.io.file.FileConnection; /** * * @author Yosia KN */ public class DownloadImage { public void download(String urlPath, String pathFolder, String filename) throws IOException { /* * example : * urlPath : http://yosiakn.lumbungilmu.com/1.png * pathFolder : file:///E:/YADS/ * fileName : 1.png */ HttpConnection http = (HttpConnection) Connector.open(urlPath); InputStream input = http.openInputStream(); ByteArrayOutputStream output = new ByteArrayOutputStream(); int ch; while ((ch = input.read()) != -1) { output.write(ch); } http.close(); FileConnection file = (FileConnection) Connector.open(pathFolder + filename); file.create(); file.openOutputStream().write(output.toByteArray()); output.close(); file.setReadable(true); file.close(); System.gc(); } }
No comments:
Post a Comment