0

私はこのチュートリアルに従いましたが、200を取得する代わりに、サーバーの応答として常に302を取得します。

turorial

誰かがこれをチェックできますか?

4

2 に答える 2

0

URLを「http://example.com」と指定すると、サーバーがリダイレクトコード302を使用してすぐに「http://example.com/」(末尾のスラッシュに注意)にリダイレクトするなど、URLで何かおかしなことをした可能性があります。 。

サーバーから返されたヘッダーを確認してください。通常、正しい場所が含まれています。

于 2012-05-08T05:35:53.587 に答える
0

アプリでこのコードを使用しました。うまくいきました。それはあなたを助けるかもしれません。

private String doFileUpload() {

    final DataOutputStream dos;

    String exiting_file_path = filePath;
    String boundary = "*****";
    String twohypens = "--";
    String lineend = "\r\n";
    int maxBufferSize = 1 * 1024 * 1024;
    int buferSize;
    int bytesRead;
    String url_string = "your URL";

    try {
        Log.d("Debug", "Start Uploading");
        FileInputStream fileInStream = new FileInputStream(
                exiting_file_path);
        URL url = new URL(url_string);
        httpUrlConn = (HttpURLConnection) url.openConnection();

        httpUrlConn.setDoInput(true);
        httpUrlConn.setDoOutput(true);
        httpUrlConn.setUseCaches(false);
        httpUrlConn.setRequestMethod("POST");
        httpUrlConn.setRequestProperty("connection", "Keep-Alive");
        httpUrlConn.setRequestProperty("content-type",
                "multipart/form-data;boundary=" + boundary);

        dos = new DataOutputStream(httpUrlConn.getOutputStream());

        dos.writeBytes(twohypens + boundary + lineend);
        dos.writeBytes("content-disposition:form-data; name=\"upload_file\"; filename=\""
                + exiting_file_path + "\"" + lineend);
        dos.writeBytes(lineend);

        int buferAvailable = fileInStream.available();
        buferSize = Math.min(buferAvailable, maxBufferSize);
        final byte[] buffer = new byte[buferSize];

        bytesRead = fileInStream.read(buffer, 0, buferSize);

        while (bytesRead > 0) {
            dos.write(buffer, 0, buferSize);
            buferAvailable = fileInStream.available();
            buferSize = Math.min(buferAvailable, maxBufferSize);
            bytesRead = fileInStream.read(buffer, 0, buferSize);
        }
        dos.writeBytes(lineend);
        dos.writeBytes(twohypens + boundary + twohypens + lineend);
        fileInStream.close();
        dos.flush();
        dos.close();

    } catch (FileNotFoundException e) {
        Toast.makeText(getApplicationContext(), "" + e, 1).show();
        e.printStackTrace();
    } catch (MalformedURLException e) {
        Toast.makeText(getApplicationContext(), "" + e, 1).show();
        e.printStackTrace();
    } catch (IOException e) {
        Toast.makeText(getApplicationContext(), "" + e, 1).show();
        e.printStackTrace();
    }

    try {
        DataInputStream inStream = new DataInputStream(
                httpUrlConn.getInputStream());

        while ((STRRRR = inStream.readLine()) != null) {
            Log.d("Debug", "Server Response " + STRRRR);
        }
        inStream.close();

    } catch (IOException e) {
        e.printStackTrace();
    }

    return STRRRR;
}
于 2012-05-08T05:05:52.263 に答える