Search code examples
imageuploadhttp-postblackberry-jde

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


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.


Solution

  • 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.