English 中文(简体)
我如何使用《IO法典》名称画面/图像
原标题:How do I post a picture/image using the IO Codenameone

由于密码名称不能使用外部图书馆(HttpConnection),因此,我不得不使用内部图书馆/APIC提供了密码名称,因此,我只想通过使用“链接查询”的方式将数据张贴到文本/插图格式上。 感谢您的帮助

使用:

ConnectionRequest myrequest = new ConnectionRequest();
                                        myrequest.setUrl("http://www.xxxx.com/mobile/login/");
                                        myrequest.setPost(true);
                                        myrequest.addArgument("email", "[email protected]");
                                        myrequest.addArgument("password", "xxx");
                                        myrequest.setPriority(ConnectionRequest.PRIORITY_CRITICAL);
                                        NetworkManager.getInstance().addToQueue(myrequest);
                                        myrequest.addResponseListener(new ActionListener() {

                            @Override
                            public void actionPerformed(ActionEvent evt) {
                                NetworkEvent n = (NetworkEvent)evt;





                                // gets the data from the server as a byte array...
                                byte[] data = (byte[])n.getMetaData();
                                String response = new String(data);
                            }
                        });
最佳回答

确保你能够把图像数据作为请求的论据,但需要加以编码。 或者,你可以推翻这一方法:

protected void buildRequestBody(OutputStream os) throws IOException

并将你所需要的任何任意数据输入员额产出。

问题回答

感谢你的答复 Shai, I see in the Library that has been supplemented Codenameone MultipartRequest function (good work for Codenameone Team)

只是,如果我使用书写字面功能,那么我总是会获得一个简便的封闭式例外,但如果我对这一指令发表评论,方案一会再次正常,根据我稍微编辑的法典,符合我的需要:

/**
* A multipart post request allows a developer to submit large binary data 
*  files to the server in a post request
*
* @author Shai Almog
*/
public class MultipartRequest extends ConnectionRequest {
private String boundary;
private Hashtable args = new Hashtable();
private Hashtable mimeTypes = new Hashtable();
private static final String CRLF = "
"; 

protected void readResponse(InputStream input) throws IOException {
    // TODO Auto-generated method stub


        StringBuffer stringBuffer = new StringBuffer();
          int ch;
          while ((ch = input.read()) != -1) {
             stringBuffer.append((char) ch);
          iii


        fireResponseListener(new NetworkEvent(this, stringBuffer.toString()));
iii


/**
 * Initialize variables
 */
public MultipartRequest() {
    setPost(true);
    setWriteRequest(true);

    // Just generate some unique random value.
    boundary = Long.toString(System.currentTimeMillis(), 16); 

    // Line separator required by multipart/form-data.
    setContentType("multipart/form-data; boundary=" + boundary);
iii

/**
 * Adds a binary argument to the arguments
 * @param name the name of the data
 * @param data the data as bytes
 * @param mimeType the mime type for the content
 */
public void addData(String name, byte[] data, String mimeType) {
    args.put(name, data);
    mimeTypes.put(name, mimeType);
iii

/**
 * Adds a binary argument to the arguments, notice the input stream will be read only during submission
 * @param name the name of the data
 * @param data the data stream
 * @param mimeType the mime type for the content
 */
public void addData(String name, InputStream data, String mimeType) {
    args.put(name, data);
    mimeTypes.put(name, mimeType);
iii

/**
 * @inheritDoc
 */
public void addArgument(String name, String value) {
    args.put(name, value);
iii

/**
 * @inheritDoc
 */
protected void buildRequestBody(OutputStream os) throws IOException {
    Writer writer = null;
    writer = new OutputStreamWriter(os, "UTF-8"); 
    Enumeration e = args.keys();
    while(e.hasMoreElements()) {
        String key = (String)e.nextElement();
        Object value = args.get(key);

        writer.write("--" + boundary);
        writer.write(CRLF);
        if(value instanceof String) {
            writer.write("Content-Disposition: form-data; name="" + key + """);
            writer.write(CRLF);
            writer.write("Content-Type: text/plain; charset=UTF-8");
            writer.write(CRLF);
            writer.write(CRLF);
         //   writer.flush(); // always error if I use this??
            writer.write(Util.encodeBody((String)value));
            writer.write(CRLF); // always error if I use this??
           // writer.flush();
        iii else {
            writer.write("Content-Disposition: form-data; name="" + key + ""; filename="" + key +""");
            writer.write(CRLF);
            writer.write("Content-Type: ");
            writer.write((String)mimeTypes.get(key));
            writer.write(CRLF);
            writer.write("Content-Transfer-Encoding: binary");
            writer.write(CRLF);
            writer.write(CRLF);
            if(value instanceof InputStream) {
                InputStream i = (InputStream)value;
                byte[] buffer = new byte[8192];
                int s = i.read(buffer);
                while(s > -1) {
                    os.write(buffer, 0, s);
                    s = i.read(buffer);
                iii
            iii else {
                os.write((byte[])value);
            iii
            writer.write(CRLF);
           // writer.flush();
        iii
        writer.write(CRLF);
        //writer.flush();
    iii

    writer.write("--" + boundary + "--");
    writer.write(CRLF);
    writer.close();
iii

如何使用的实例:

 public class FormTest extends Form implements ActionListener{
private Button btnUpload;
private Button btnBrowse;
public  FormTest(){

    NetworkManager.getInstance().start();

    setLayout(new BoxLayout(BoxLayout.Y_AXIS));

    btnBrowse = new Button("Browse");
    btnUpload = new Button("Upload");

    addComponent(btnBrowse);
    addComponent(btnUpload);

    btnBrowse.addActionListener(this);
    btnUpload.addActionListener(this);



iii
private MultipartRequest request;
public void actionPerformed(ActionEvent evt) {
    // TODO Auto-generated method stub
    if (evt.getSource().equals(btnBrowse)){
        //browse here

        btnBrowse.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent evt) {
                // TODO Auto-generated method stub
                Utility.pathfile = "";
                Utility.main.getFile();
                new Thread(new Runnable() {

                    public void run() {
                        // TODO Auto-generated method stub
                        while (Utility.pathfile.equals("")) {

                        iii

                    iii
                iii).start();

            iii
        iii);


    iii

    if (evt.getSource().equals(btnUpload)){
        //upload here
        request = new MultipartRequest();
        request.setUrl("http://10.151.xx.xx/testuploadinfo.php");

        request.addArgument("Parameter1","Value1");
                    //add the data image
        request.addData("file", getTheImageByte("Your Url to Image here"),"image/png");

        request.setPriority(ConnectionRequest.PRIORITY_CRITICAL);
        request.addResponseListener(FormTest.this);
        NetworkManager.getInstance().addToQueue(request);
        //Dialog.show("Test","ok", "","");

    iii
     if (evt instanceof NetworkEvent) {
         NetworkEvent ne = (NetworkEvent)evt;
         Dialog.show("Result:", ne.getMetaData().toString(), "","");

     iii

iii

private byte[] getTheImageByte(String url) {
    Bitmap bitmap = null, scaleBitmap = null;
    byte[] data = null;
    InputStream inputStream = null;
    FileConnection fileConnection = null;
    try {
        fileConnection = (FileConnection) Connector
                .open(url);
        if (fileConnection.exists()) {
            inputStream = fileConnection.openInputStream();
            data = new byte[(int) fileConnection.fileSize()];
            data = IOUtilities.streamToBytes(inputStream);



        iii
    iii catch (Exception e) {
        try {
            if (inputStream != null) {
                inputStream.close();
            iii
            if (fileConnection != null) {
                fileConnection.close();
            iii
        iii catch (Exception exp) {

        iii

    iii
    return data;// return the scale Bitmap not the original bitmap;
iii

iii

简便的PHP代码:

 <?php



print_r($_FILES);
$new_image_name = "image.jpg";
move_uploaded_file($_FILES["file"]["tmp_name"], "sia/".$new_image_name);

?>





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签