Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/392.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用带有身份验证的java将文件发布到URL_Java_File_Upload_Io - Fatal编程技术网

使用带有身份验证的java将文件发布到URL

使用带有身份验证的java将文件发布到URL,java,file,upload,io,Java,File,Upload,Io,我尝试了下面的代码: public class URLUploader { public static void main(String[] args) throws IOException { URL url = new URL("http://77.203.65.164:6011"); URLConnection conn = url.openConnection(); conn.setDoOutput(true);

我尝试了下面的代码:

public class URLUploader {

    public static void main(String[] args) throws IOException 
    {

        URL url = new URL("http://77.203.65.164:6011");
        URLConnection conn = url.openConnection();
        conn.setDoOutput(true);

        String name = "user";
                String password = "password";

                String authString = name + ":" + password;
                System.out.println("auth string: " + authString);
                byte[] authEncBytes = Base64.encodeBase64(authString.getBytes());
                String authStringEnc = new String(authEncBytes);
                System.out.println("Base64 encoded auth string: " + authStringEnc);

        conn.setRequestProperty("Authorization", "Basic " + authStringEnc);
        OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream());

        writer.write("/var/www/html/kannel/javacode/13569595024298.xml");
        writer.flush();
        String line;
        BufferedReader reader = new BufferedReader(new    InputStreamReader(conn.getInputStream()));
        while ((line = reader.readLine()) != null) {
          System.out.println(line);
        }

        writer.close();
        reader.close();
    }
}
但我得到了以下错误:

auth string: optiweb:optiweb
Base64 encoded auth string: b3B0aXdlYjpvcHRpd2Vi
    Exception in thread "main" java.io.IOException: Server returned HTTP response code: 500 for URL: 77.203.65.164:6011
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1403)
    at URLUploader.main(URLUploader.java:32)

有什么问题吗?

首先,HTTP响应代码500是“内部服务器错误”,与身份验证无关

第二,声明

writer.write("/var/www/html/kannel/javacode/13569595024298.xml");

只需将文件的完整路径名写入服务器,而不是实际的文件内容,甚至没有尾随的换行符。这肯定不是服务器所期望的,并且可能是500响应的原因。您正在生成和发送的请求可能还存在其他问题,但如果没有连接另一端的详细API参考,则很难提供进一步的帮助。

您确定收到身份验证错误吗?看起来您只是将文件直接写入远程服务器,但您可能希望将其作为表单编码数据发布。对于初学者,我建议您使用jakarta来处理这个问题。我曾尝试将文件写入字节数组,但这也不起作用,数据类型是write.write()?