使用blackberry http连接将图像上传到服务器

时间:2012-10-18 23:20:34

标签: blackberry file-upload blackberry-eclipse-plugin httpconnection multipartform-data

嗨下面是将文件上传到服务器的简单代码,它的工作正常......但是当我通过黑莓发送图像到服务器时它做了一些工作,但服务器中没有图像文件,请指导我在哪里犯错误..

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title></title>
</head>

<body>
    <form enctype="multipart/form-data" method="post" action="img.php">
        <input type="file" name="image"/>
        <input type="submit" name="upload"/>
    </form>
        </body>
</html>

这是bb

的java代码
public HttpMultipartRequest(String url, Hashtable params, Hashtable headers, 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());

        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");

        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).append(
                            "\r\n").append("--").append(boundary)
                    .append("\r\n");
        }
        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();
    }

    public byte[] send() 
    {
        HttpConnection hc = null;

        InputStream is = null;

        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        String login = test:test;
        byte[] res = null;

        boolean keepGoing = true;
        boolean needAuth = false;

        try {
            byte[] encoded = Base64OutputStream.encode(login.getBytes(), 0, login.length(), false, false);

            Logger.log("Trying to connect to "+url);
            hc = (HttpConnection) Connector.open(url);

            while (keepGoing) {
                // headers needed specifically for upload
                hc.setRequestProperty("Content-Type",
                        "multipart/form-data; boundary=" + getBoundaryString());
                hc.setRequestProperty(
                        HttpProtocolConstants.HEADER_CONTENT_LENGTH, Integer
                                .toString(postBytes.length));
                hc.setRequestProperty("x-rim-transcode-content", "none");

                hc.setRequestProperty("Authorization", "Basic " + new String(encoded));


                hc.setRequestMethod(HttpConnection.POST);

                OutputStream dout = hc.openOutputStream();


                dout.write(postBytes);

                dout.close();

                int responseCode = hc.getResponseCode();
                if (responseCode == HttpConnection.HTTP_UNAUTHORIZED) {
                    hc.close();
                    hc = (HttpConnection) Connector.open(url);
                    needAuth = true;

                    throw new Exception("Exception when uploading. RC: "+String.valueOf(responseCode)+", Unauthorized.");

                } else if (responseCode == 422) {
                    throw new Exception("Exception when uploading. RC: "+String.valueOf(responseCode)+".");
                } else if (responseCode == HttpConnection.HTTP_OK) {
                    int ch;
                    bos.reset();

                    is = hc.openInputStream();

                    while ((ch = is.read()) != -1) {
                        bos.write(ch);
                    }
                    res = bos.toByteArray();

                    keepGoing = false;
                } else {
                    int ch;
                    bos.reset();
                    is = hc.openInputStream();

                    while ((ch = is.read()) != -1) {
                        bos.write(ch);
                    }
                    res = bos.toByteArray();

                    keepGoing = false;
                    throw new Exception("Exception when uploading. RC: "+String.valueOf(responseCode)+", "+new String(res));
                }

            }

1 个答案:

答案 0 :(得分:2)

前几天,我遇到了同样的问题,现在我在下面link的帮助下解决了这个问题......可能这会对你有所帮助.. !!!