Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/308.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.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_Url - Fatal编程技术网

Java-写入url不起作用

Java-写入url不起作用,java,url,Java,Url,我正在尝试制作一个java程序来更改我网站上的文本文档。每个人都可以对其进行编辑。我试过了,读起来很好,但写作不行。 以下是编写代码: import java.net.*; import java.io.*; public class Main { public static void main(String[] args) throws Exception { URL infoThing = new URL("http://www.[name of my website

我正在尝试制作一个java程序来更改我网站上的文本文档。每个人都可以对其进行编辑。我试过了,读起来很好,但写作不行。 以下是编写代码:

import java.net.*;
import java.io.*;
public class Main {
    public static void main(String[] args) throws Exception {
        URL infoThing = new URL("http://www.[name of my website]/infoThing.txt");
        URLConnection con = infoThing.openConnection();
        con.setDoOutput(true);
        OutputStreamWriter out = new OutputStreamWriter(con.getOutputStream());
        out.write("Change to this.");
        out.close();
    }
}

对于要与服务器端进程交互的Java程序,它必须能够写入URL,从而向服务器提供数据。它可以通过以下步骤完成此操作:

1创建一个URL

2检索URLConnection对象

3在URLConnection上设置输出能力

4打开与资源的连接

5从连接获取输出流

6写入输出流

7关闭输出流

如果要写入url。您必须使用上面的概念和servlet的概念。 通过URLConnection在网络上运行向后脚本的示例程序:

import java.io.*;
import java.net.*;

public class ReverseTest {
    public static void main(String[] args) {
        try {
            if (args.length != 1) {
                System.err.println("Usage:  java ReverseTest string_to_reverse");
                System.exit(1);
            }
            String stringToReverse = URLEncoder.encode(args[0]);

            URL url = new URL("http://java.sun.com/cgi-bin/backwards");
            URLConnection connection = url.openConnection();

            PrintStream outStream = new PrintStream(connection.getOutputStream());
            outStream.println("string=" + stringToReverse);
            outStream.close();

            DataInputStream inStream = new DataInputStream(connection.getInputStream());
            String inputLine;

            while ((inputLine = inStream.readLine()) != null) {
                System.out.println(inputLine);
            }
            inStream.close();
        } catch (MalformedURLException me) {
            System.err.println("MalformedURLException: " + me);
        } catch (IOException ioe) {
            System.err.println("IOException: " + ioe);
        }
    }
}

一般来说,web应用程序不是这样工作的。Google docs让它看起来很简单,但协作编辑相当复杂。如果你想动态更改网页上的内容,你需要使用javascript。不正确,sunrize920。Java也能做到这一点@user3877416您编写的代码不是url编写器。。我只是向您展示如何编写正确的url代码。我已经提供和链接。在那里你可以了解它。你应该接受我的回答:如果你读了我的代码,你在网站上读了代码,你会发现,除了反转、读取和变量名,这两者是一样的。我在寻找它为什么不起作用;如果我从oracle站点尝试代码,它仍然不起作用。