Blackberry jde : how to upload an image in server using MultipartPostData

瘦欲@ 提交于 2019-12-25 14:40:55

问题


Here is the problem I am trying to send an image to a remote server using the class MultipartPostData.

I build my PostData with the following code :

PostData body = new MultipartPostData(MultipartPostData.DEFAULT_CHARSET, false);
body.append("deviceID", ""+DeviceInfo.getDeviceId());
body.append("synchro", "true");
body.append("shoot_lat", ""+latitude);
body.append("shoot_long", ""+longitude);
body.append("shoot_place", ""+city);
body.append("shoot_time", String.valueOf(time));
body.append("ref_data", "filename=\"photo.jpg\"");
//image is a byte[]
body.setData(image);

I think something is wrong in the way of setting the image because the server can not get this body (if I print the data received and analysed by the server in the body of the request, I get a blank string).

Can anybody help to build a working request to send an image ?

Thanks a lot.


回答1:


Ok, I finally found the answer by myself.

    private static final String BOUNDARY = "---------------------------14737809831466499882746641449";

public void createHttpMultipartRequest(String url, Hashtable params, String fileField, String fileName, String fileType, byte[] fileBytes) throws Exception
{
    this.url = url;
    String boundary = getBoundaryString();
    String boundaryMessage = getBoundaryMessage(boundary, params, fileField, fileName, fileType);

    String endBoundary = "\r\n--" + boundary + "--\r\n";
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    bos.write(boundaryMessage.getBytes());

    if(fileField != null)
    {
        bos.write(fileBytes);
    }
    bos.write(endBoundary.getBytes());

    this.postBytes = bos.toByteArray();
    bos.close();
}

String getBoundaryString()
{
    return BOUNDARY;
}

String getBoundaryMessage(String boundary, Hashtable params, String fileField, String fileName, String fileType)
{
    StringBuffer res = new StringBuffer("--").append(boundary).append("\r\n");
    String intermediateBoundary = "\r\n--" + boundary + "\r\n";

    Enumeration keys = params.keys();
    while(keys.hasMoreElements())
    {
        String key = (String)keys.nextElement();
        String value = (String)params.get(key);
        res.append("Content-Disposition: form-data; name=\"").append(key).append("\"\r\n")    
            .append("\r\n").append(value);

        if(keys.hasMoreElements())
        {
            res.append(intermediateBoundary);
        }
    }

    if(fileField != null)
    {
        res.append(intermediateBoundary);
        res.append("Content-Disposition: form-data; name=\"").append(fileField).append("\"; filename=\"").append(fileName).append("\"\r\n") 
            .append("Content-Type: ").append(fileType).append("\r\n\r\n");
    }
    return res.toString();
}

The Hashtable contains all the parameters of the form. And the fileBytes contains the data to send.



来源:https://stackoverflow.com/questions/5342577/blackberry-jde-how-to-upload-an-image-in-server-using-multipartpostdata

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!