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

时间:2015-03-27 21:30:19

标签: java php file-upload windows-7

我有一个Java程序。它在Windows XP,Windows 8和Windows 10上运行良好。但在Windows 7上,当我尝试上传txt文件时出错。最糟糕的是,当我在IDE(BlueJ)中执行它时,Win 7上的程序会很好地上传文件,而且当我在IDE之外正常执行它时它也没有。 我在网站上的PHP中有这个代码,用于存储上传的文件(upload.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()];

2 个答案:

答案 0 :(得分:0)

尝试检查上一行

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

看起来像getClass()。getResourceAsStream(“/”+ ficRecordes);正在返回一个空引用。有关此命令的stackoverflow有很多答案。尝试在那里寻找答案。

答案 1 :(得分:0)

替换

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

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