通过HttpURLClient将图像上传到Web服务器不起作用

时间:2017-08-19 13:44:40

标签: java php httpurlconnection

我正在尝试使用以下代码将图像从设备存储上传到本地网络服务器:

public class ImageUpload {

    static String LINE_END = "\r\n";
    static String TWO_HYPHENS = "--";
    static String BOUNDARY = "*****";

    public static void main(String[] args) {
        Scanner mScanner = new Scanner(System.in);

        System.out.println("Path of the Image:");
        String sPath = mScanner.nextLine();

        HttpURLConnection mConnection;
        //DataInputStream mInput;
        DataOutputStream mOutput;
        int iBytesRead, iBytesAvailable, iBufferSize;
        byte[] mByteBuffer;
        int iMaxBufferSize = 1024*1024;

        String sUploadURL = "http://172.16.0.15/hsay/upload/";

        try {
            FileInputStream mInputStream = new FileInputStream(new File(sPath));
            URL mUrl = new URL(sUploadURL);

            mConnection = (HttpURLConnection) mUrl.openConnection();
            mConnection.setDoInput(true);
            mConnection.setDoOutput(true);
            mConnection.setUseCaches(true);

            mConnection.setRequestMethod("POST");
            mConnection.setRequestProperty("Connection", "Keep-Alive");
            mConnection.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + BOUNDARY);

            mOutput = new DataOutputStream(mConnection.getOutputStream());
            mOutput.writeBytes(TWO_HYPHENS + BOUNDARY + LINE_END);
            mOutput.writeBytes("Content-Disposition: form-data); name=\"image\";filename=\"" + new File(sPath).getName() + "\"" + LINE_END);
            mOutput.writeBytes(LINE_END);

            iBytesAvailable = mInputStream.available();
            iBufferSize = Math.min(iBytesAvailable, iMaxBufferSize);
            mByteBuffer = new byte[iBufferSize];
            iBytesRead = mInputStream.read(mByteBuffer, 0, iBufferSize);

            while (iBytesRead > 0) {
                mOutput.write(mByteBuffer, 0, iBufferSize);
                iBytesAvailable = mInputStream.available();
                iBufferSize = Math.min(iBytesAvailable, iMaxBufferSize);
                iBytesRead = mInputStream.read(mByteBuffer, 0, iBufferSize);
            }

            mOutput.writeBytes(LINE_END);
            mOutput.writeBytes(TWO_HYPHENS + BOUNDARY + TWO_HYPHENS + LINE_END);

            BufferedReader mInputReader = new BufferedReader(new InputStreamReader(mConnection.getInputStream()));
            StringBuilder mStringBuilder = new StringBuilder();
            String sInputLine;
            while ((sInputLine = mInputReader.readLine()) != null) {
                mStringBuilder.append(sInputLine);
            }

            System.out.println(mStringBuilder.toString());

            mInputStream.close();
            mOutput.flush();
            mOutput.close();
        }
        catch (MalformedURLException mue) {
            mue.printStackTrace();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }

        mScanner.close();
    }
}

我的PHP代码:

$mUploadedFile = $_FILES['image']['name'];
$mUploadedFileType = $_FILES['image']['type'];
$mTempFile = $_FILES['image']['tmp_name'];
$mError = $_FILES['image']['error'];


if ($mError > 0) {
    die("[ERROR]: File couldn't be uploaded.".$mError);
}
else {
    move_uploaded_file($mTempFile, "images/".$mUploadedFile);
    echo "[SUCCESS UPLOAD]: ".$mUploadedFileType;
}
?>

页面输出为“[SUCCESS UPLOAD]”,但文件未上传或我找不到!

方法var_dump($_FILES)返回以下内容:

  

array(1){[“image”] => array(5){[“name”] => string(8)“Logo.png”[“type”] => string(0)“”[“tmp_name”] => string(14)“/ tmp / phpyxwnos”[“error”] => int(0)[“size”] => int(567086)}}

输入路径为C:\ Users \ Daniel \ Logo.png。

我在笔记本上用Eclipse做过这个测试,它应该在Android应用程序中实现。本地网络服务器是运行Apache2.0的Raspberry Pi 3!

有谁知道什么是错的? 提前谢谢!

0 个答案:

没有答案