-1

こんにちは、以下はファイルをサーバーにアップロードするための簡単なコードです。正常に動作します...しかし、Blackberry経由でサーバーに画像を送信すると、いくつかの作業が行われますが、サーバーに画像ファイルがありません。間違いを犯している場所を教えてください...

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

            }
4

1 に答える 1

2

数日前、私は同じ問題に直面しました。今、私は次のリンクの助けを借りてこれを整理しました...これがあなたを助けるかもしれません..!!!

于 2012-10-19T11:20:20.567 に答える