以文本方式查看主题

-  课外天地 李树青  (http://njcie.com/bbs/index.asp)
--  JavaME移动开发课件  (http://njcie.com/bbs/list.asp?boardid=18)
----  程序代码——上传图片  (http://njcie.com/bbs/dispbbs.asp?boardid=18&id=587)

--  作者:admin
--  发布时间:2008/10/30 21:11:39
--  程序代码——上传图片

JSP网页:

<%@page c%>
<%@page import="java.io.*" %>
<%@page import="javax.imageio.ImageIO" %>
<%@page import="java.awt.image.BufferedImage" %>

<html>
    <head>
        <title>演示</title>
    </head>
    
    <body>
        <%
        DataInputStream dis = new DataInputStream(request.getInputStream());
        int width= dis.readInt();
        int height= dis.readInt();
        int length=width*height;
        int[] array=new int[length];
        for (int i = 0; i < array.length; i++)
            array [ i ]=dis.readInt();
        BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        bi.setRGB(0,0,width,height,array,0,width);
        bi.flush();
        String sessionID = (String) session.getId();
        File f = new File(application.getRealPath("/" + sessionID + ".jpg"));
        ImageIO.write(bi, "jpg", f);
        %>
    </body>
</html>

MIDLET程序:
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;

import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.TextBox;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;

public class Exec extends MIDlet {
        Display display = null;
        TextBox tb = null;

        public Exec() {
                String str = getNetInfo();
                try {
                        str = new String(str.getBytes("ISO-8859-1"), "GB2312");
                } catch (UnsupportedEncodingException e) {
                        System.out.println(e.getMessage());
                }

                tb = new TextBox("测试", str, str.length(), TextField.ANY);
                display = Display.getDisplay(this);
        }

        public void startApp() {
                display.setCurrent(tb);
        }

        public void pauseApp() {
        }

        public void destroyApp(boolean unconditional) {
        }

        public String getNetInfo() {
                StringBuffer sb = new StringBuffer();
                HttpConnection httpConn = null;
                InputStream is = null;
                DataOutputStream dos = null;
                try {
                        httpConn = (HttpConnection) Connector
                                        .open("http://localhost:8084/WebApplication2/index.jsp");
                        httpConn.setRequestMethod("POST");
                        dos = new DataOutputStream(httpConn.openOutputStream());

                        Image image = Image.createImage("/images/java.jpg");
                        int width = image.getWidth();
                        int height = image.getHeight();
                        int[] imageArray = new int[width * height];
                        image.getRGB(imageArray, 0, width, 0, 0, width, height);
                        dos.writeInt(width);
                        dos.writeInt(height);
                        for (int i = 0; i < imageArray.length; i++)
                                dos.writeInt(imageArray [ i ]);
                        dos.close();
                        int status = httpConn.getResponseCode();
                        if (status != HttpConnection.HTTP_OK)
                                throw new IOException("Response code not ok");
                        else {
                                is = httpConn.openInputStream();
                                int ch;
                                while ((ch = is.read()) != -1) {
                                        sb.append((char) ch);
                                }
                        }
                        httpConn.close();
                } catch (IOException e) {
                }
                return sb.toString();
        }
}


 

[此贴子已经被作者于2010-12-12 18:44:19编辑过]