Java 在 Windows 上通过 PHP 上传文件时出现程序错误 7

Java program error in uploading file throught PHP on Windows 7

我有一个我制作的 Java 程序。它在 Windows XP、Windows 8 和 Windows 10 上工作正常。但是在 Windows 7 上,当我尝试让它上传 txt 文件时出现错误。最糟糕的是,当我在 IDE (BlueJ) 中执行它时,Win 7 上的程序可以漂亮地上传文件,而当我在 IDE.
之外正常执行时却不会。 我在存储上传文件 (upload.php) 的网站 PHP 中有此代码:

<?php 
$target_path = "./";
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']); 

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) { 
    echo "The file ". basename( $_FILES['uploadedfile']['name'])." has been uploaded"; 
} else{ 
    echo "There was an error uploading the file, please try again!"; 
}
?>

错误不应该来自服务器端的 php 但我还是把它放了。
这是上传的 java 代码:

public void uploadRecordes()
    {
        final String ficRecordes = "recordes.txt";
        final String siteURL = "http://example.com/"
        final String CrLf = "\r\n";
        URLConnection conn = null;
        OutputStream os = null;
        InputStream is = null;

        try {
            URL url = new URL(siteURL + "upload.php");
            //System.out.println("url:" + url);
            conn = url.openConnection();
            conn.setDoOutput(true);

            String postData = "";

            InputStream imgIs = getClass().getResourceAsStream("/" + ficRecordes);
            byte[] imgData = new byte[imgIs.available()];
            imgIs.read(imgData);
            imgIs.close();

            String message1 = "";
            message1 += "-----------------------------4664151417711" + CrLf;
            message1 += "Content-Disposition: form-data; name=\"uploadedfile\"; filename=\"" +
            ficRecordes + "\"" + CrLf;
            message1 += "Content-Type: text/txt" + CrLf;
            message1 += CrLf;

            // the image is sent between the messages in the multipart message.

            String message2 = "";
            message2 += CrLf + "-----------------------------4664151417711--"
                    + CrLf;

            conn.setRequestProperty("Content-Type",
                    "multipart/form-data; boundary=---------------------------4664151417711");
            // might not need to specify the content-length when sending chunked
            // data.
            conn.setRequestProperty("Content-Length", String.valueOf((message1
                    .length() + message2.length() + imgData.length)));

            //System.out.println("open os");
            os = conn.getOutputStream();

            //System.out.println(message1);
            os.write(message1.getBytes());

            // SEND THE IMAGE
            int index = 0;
            int size = 1024;
            do {
                //System.out.println("write:" + index);
                if ((index + size) > imgData.length) {
                    size = imgData.length - index;
                }
                os.write(imgData, index, size);
                index += size;
            } while (index < imgData.length);
            //System.out.println("written:" + index);

            //System.out.println(message2);
            os.write(message2.getBytes());
            os.flush();

            //System.out.println("open is");
            is = conn.getInputStream();

            char buff = 512;
            int len;
            byte[] data = new byte[buff];
            do {
                //System.out.println("READ");
                len = is.read(data);

                if (len > 0) {
                    //System.out.println(new String(data, 0, len));
                }
            } while (len > 0);

            //System.out.println("DONE");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //System.out.println("Close connection");
            try {
                os.close();
            } catch (Exception e) {
            }
            try {
                is.close();
            } catch (Exception e) {
            }
        }
    }

它在 Win 7 上给我的错误是 NullPointerException。 并且出现at Game.uploadRecordes(Game.java:149)。这意味着错误在第 149 行?如果是这样,该行是

byte[] imgData = new byte[imgIs.available()];

尝试检查前一行

InputStream imgIs = getClass().getResourceAsStream("/" + ficRecordes);

好像getClass().getResourceAsStream("/" + ficRecordes);正在返回空引用。与此命令相关的 Whosebug 上有很多答案。尝试在那里搜索答案。

替换

InputStream imgIs = getClass().getResourceAsStream("/" + ficRecordes);

FileInputStream imgIs = new FileInputStream(new File(ficRecordes));