课外天地 李树青学习天地JavaME移动开发课件 → 程序代码——上传图片


  共有15426人关注过本帖树形打印复制链接

主题:程序代码——上传图片

帅哥哟,离线,有人找我吗?
admin
  1楼 博客 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信 管理员
等级:管理员 帖子:1949 积分:26803 威望:0 精华:34 注册:2003/12/30 16:34:32
程序代码——上传图片  发帖心情 Post By: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编辑过]

 回到顶部