⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 uploadform.java

📁 手机上传图片到服务器 访问web地址 不错的小程序
💻 JAVA
字号:
package com;
/*
 * UploadForm.java
 *
 * Created on 2007年4月13日, 下午4:12
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

/**
 *
 * @author chenzs
 */
import javax.microedition.io.*;
import javax.microedition.lcdui.*;
import java.io.*;

public class UploadForm extends DynamicForm implements CommandListener{
    
    String picPath;
    
    
    /** Creates a new instance of UploadForm */
    public UploadForm(String title, String path) {
        super(title);
        picPath = path;
        
        this.addCommand(new Command("Cancel", Command.CANCEL, 1));
        this.setCommandListener(this);
               
        start();
        (new HttpConnThread()).start();
    }
    
    //读取照片数据
    private byte[] readPicsBytes() throws IOException{
        byte[] buf = null;
        InputStream is = null;
        DataInputStream dis = null;
        try{
           is = HttpMidlet.instance.getClass().getResourceAsStream(picPath);
           dis = new DataInputStream(is);
           int len = dis.available();
           buf = new byte[len];
           dis.read(buf);
          
        }
        finally{
            if(dis != null)
                dis.close();
//            if(is != null)
//                is.close();
        }   
        return buf;
    }
     
    /*
     *
     */
    public void getHttpConnection(){
       HttpConnection hc = null;
       InputStream is = null;
       DataOutputStream os = null;
       int rc;
       try {
           //read image data
           byte [] data = readPicsBytes();
          // System.out.println(new String(data));
            hc = (HttpConnection)Connector.open(HttpMidlet.strUploadURL, Connector.READ_WRITE);
            // Set the request method and headers
            hc.setRequestMethod(HttpConnection.POST);
//            hc.setRequestProperty("If-Modified-Since",
//                "29 Oct 1999 19:43:31 GMT");
            hc.setRequestProperty("User-Agent","Mozilla/4.0");
            hc.setRequestProperty("Content-Language", "en-US");
            hc.setRequestProperty("Content-Type","image/png");
//            hc.setRequestProperty("Accept-Encoding", "gzip");
//            hc.setRequestProperty("Transfer-Encoding", "unchunked");
            hc.setRequestProperty("Content-Length", String.valueOf(data.length));
            // Getting the output stream may flush the headers
            os = hc.openDataOutputStream();
            os.write(data, 0, data.length);
            //os.write("This is a string".getBytes());
            // Optional, if getResponseCode will flush
           //  os.flush();           
 
            // Getting the response code will open the connection,
            rc = hc.getResponseCode();
            stop(); //end animation thread
            label.setLabel("Finished !");
            StringItem msgResp = new StringItem("Response Code", ":" + rc);
            append(msgResp);//show response code
            if(rc == 200){
                append("图片上传成功!");
                Image img = Image.createImage(data, 0, data.length);
                append(img);
            }
            
//            
//            is = hc.openInputStream();
//            DataInputStream dis = new DataInputStream(is);
//            int length = dis.available();
//            byte[] databuf = new byte[length];
//            dis.read(databuf);
//            append(new String(databuf));//show response data
////            close ....
//            is.close();
            os.close();
            hc.close();
        } catch (IOException e) {
            e.printStackTrace();
            append(e.toString());//show error msg.
        }
      
    }//end method
    
    /*
     * softkey command action method
     */
    public void commandAction(Command cmd, Displayable dip){
        if(dip != this)
            return;
        if(cmd.getCommandType()==Command.CANCEL){
            stop();
            HttpMidlet.instance.showMainList();
        }
       
    }
    
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -