【问题标题】:Java program error in uploading file throught PHP on Windows 7在 Windows 7 上通过 PHP 上传文件时出现 Java 程序错误
【发布时间】:2015-06-01 08:40:39
【问题描述】:

我制作了一个 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()];

【问题讨论】:

    标签: java php file-upload windows-7


    【解决方案1】:

    尝试检查前一行

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

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

    【讨论】:

    • 我尝试了很多新方法,但没有任何效果。问题是在IDE内部执行时没有出现这个错误
    • 因为 IDE 设置类路径的方式与 vm 正常设置的方式不同。 Recordes.txt 是在罐子里还是在其他地方?尝试将recordes.txt存储在C:/然后说InputStream imgIs = new File("C:/recordes.txt").getInputStream();再试一次
    • 谢谢。我已经使用FileInputStream imgIs = new FileInputStream(new File(ficRecordes));
    【解决方案2】:

    替换

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-03-21
      • 2012-02-09
      • 1970-01-01
      • 2021-08-09
      • 1970-01-01
      • 2017-10-12
      • 2013-10-17
      相关资源
      最近更新 更多