Java 打开远程文件并写入
我在我的服务器上打开一个txt文件,获取Int,并希望将Int增加1,然后再次将其写入该文件 我使用以下方法获取文件:Java 打开远程文件并写入,java,http,file-io,Java,Http,File Io,我在我的服务器上打开一个txt文件,获取Int,并希望将Int增加1,然后再次将其写入该文件 我使用以下方法获取文件: public int getCount() { try { URL updateURL = new URL("http://myserver.gov/text.txt"); URLConnection conn = updateURL.openConnection(); InputStream is = conn.getInputStream(); Bu
public int getCount() {
try {
URL updateURL = new URL("http://myserver.gov/text.txt");
URLConnection conn = updateURL.openConnection();
InputStream is = conn.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
ByteArrayBuffer baf = new ByteArrayBuffer(50);
int current = 0;
while((current = bis.read()) != -1){
baf.append((byte)current);
}
/* Convert the Bytes read to a String. */
String tmp = new String(baf.toByteArray());
int count = Integer.valueOf(tmp);
return count;
} catch(Exception e) {
Log.e(TAG, "getAdCount Exception = " + e);
e.printStackTrace();
return -1;
}
}
BufferedWriter out = new BufferedWriter(new FileWriter("text.txt"));
out.write(count);
out.close();
现在我只需增加计数,并将其写入文件。
我发现,使用此方法可以写入文件:
public int getCount() {
try {
URL updateURL = new URL("http://myserver.gov/text.txt");
URLConnection conn = updateURL.openConnection();
InputStream is = conn.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
ByteArrayBuffer baf = new ByteArrayBuffer(50);
int current = 0;
while((current = bis.read()) != -1){
baf.append((byte)current);
}
/* Convert the Bytes read to a String. */
String tmp = new String(baf.toByteArray());
int count = Integer.valueOf(tmp);
return count;
} catch(Exception e) {
Log.e(TAG, "getAdCount Exception = " + e);
e.printStackTrace();
return -1;
}
}
BufferedWriter out = new BufferedWriter(new FileWriter("text.txt"));
out.write(count);
out.close();
但是我如何打开远程文件呢?我找不到办法。谢谢
编辑:我编写了以下代码:
URL url = new URL("http://myserver.gov/text.txt");
URLConnection con = url.openConnection();
con.setDoOutput(true);
OutputStreamWriter out = new OutputStreamWriter(con.getOutputStream());
out.write(count);
out.close();
但它不会将计数写入文件 根据我的说法。当你打开远程文件时。首先你必须打开连接,而不是读取文件内容
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
然后你可以写文件内容。根据我的说法。当你打开远程文件时。首先你必须打开连接而不是读取文件内容
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
您可以编写文件内容。当您想使用URLConnection时,可以按照以下说明操作:
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
更新:您还需要一台正在运行的服务器来处理POST请求以更新计数器。当您想使用URLConnection时,可以按照以下说明操作:
更新:您还需要一台正在运行的服务器来处理POST请求,以更新您的计数器。谢谢。我使用了引用,并编写了以下代码:
URL=newurl(“http://myserver.gov/text.txt"); URLConnection con=url.openConnection();con.设置输出(真);OutputStreamWriter out=新的OutputStreamWriter(con.getOutputStream());写出(数);out.close()代码>但他们没有将计数写入其中。哪个服务器正在运行?它如何处理post请求?当您使用URLConnection时,通常会实例化HttpURLConnection()。然后通过http请求(如GET和POST)与服务器通信。当您正在执行POST请求时,当您的服务器不知道如何处理POST请求时,这将不起作用!我假设“简单Web空间”就是这种情况)。当您查看答案链接中的示例时,您会注意到server.Nah上运行着一个servlet。好的,我还得写一个服务器应用程序。谢谢,谢谢。我使用了引用,并编写了以下代码:URL=newurl(“http://myserver.gov/text.txt"); URLConnection con=url.openConnection();con.设置输出(真);OutputStreamWriter out=新的OutputStreamWriter(con.getOutputStream());写出(数);out.close()代码>但他们没有将计数写入其中。哪个服务器正在运行?它如何处理post请求?当您使用URLConnection时,通常会实例化HttpURLConnection()。然后通过http请求(如GET和POST)与服务器通信。当您正在执行POST请求时,当您的服务器不知道如何处理POST请求时,这将不起作用!我假设“简单Web空间”就是这种情况)。当您查看答案链接中的示例时,您会注意到server.Nah上运行着一个servlet。好的,我还得写一个服务器应用程序。谢谢。我的错,有一秒钟我以为你必须把一个文件传输到另一台服务器,我会删除我的评论。我的错,有一秒钟我以为你必须把一个文件传输到另一台服务器,我会删除我的评论