用途:使用Android HttpClient在异步任务的doInBackground()中将文件上载到tomcat服务器
在这里和那里找到了几个链接。。但是没有一个为我工作。 努力实现以下目标: 1) 我在位置服务器地址中指定了一个服务器(例如:www.google.com:8080) 2) 服务器有一个php脚本来处理输入。 3) 想要在服务器中的位置/var/www/working创建一个文本文件/ 4) 内容存储在一个字符串URL文件中(这是一个结构化json) 5) 将字符串直接写入服务器位置,而不是弄乱用户的设备用途:使用Android HttpClient在异步任务的doInBackground()中将文件上载到tomcat服务器,android,apache,fileoutputstream,httpconnection,androidhttpclient,Android,Apache,Fileoutputstream,Httpconnection,Androidhttpclient,在这里和那里找到了几个链接。。但是没有一个为我工作。 努力实现以下目标: 1) 我在位置服务器地址中指定了一个服务器(例如:www.google.com:8080) 2) 服务器有一个php脚本来处理输入。 3) 想要在服务器中的位置/var/www/working创建一个文本文件/ 4) 内容存储在一个字符串URL文件中(这是一个结构化json) 5) 将字符串直接写入服务器位置,而不是弄乱用户的设备 private class WebServiceCall extends AsyncTas
private class WebServiceCall extends AsyncTask<String, Void, String>{
protected String doInBackground(String... urls) {
try {
URL url = new URL(locationServer + "/handle.php");
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches(false);
// Setting chunk improves performance
conn.setChunkedStreamingMode(0);
//Enabling POST method
((HttpURLConnection) conn).setRequestMethod("POST");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
FileOutputStream serverWriter = new FileOutputStream(new File("/var/www/working/jsonFile.txt"), false);
serverWriter.write(urlFile.getBytes());
serverWriter.close();
conn.disconnect();
}
catch(IOException e){
e.printStackTrace();
return "Server Refused";
}
return "Uploaded";
}
私有类WebServiceCall扩展异步任务{
受保护的字符串doInBackground(字符串…URL){
试一试{
URL=新URL(locationServer+“/handle.php”);
HttpURLConnection conn=(HttpURLConnection)url.openConnection();
连接设置输出(真);
conn.setDoInput(真);
conn.SETUSECHACHES(假);
//设置块可以提高性能
conn.setChunkedStreamingMode(0);
//启用POST方法
(HttpURLConnection)conn.setRequestMethod(“POST”);
conn.setRequestProperty(“连接”、“保持活动”);
conn.setRequestProperty(“内容类型”、“多部分/表单数据;边界=“+boundary”);
FileOutputStream serverWriter=newfileoutputstream(新文件(“/var/www/working/jsonFile.txt”),false;
write(urlFile.getBytes());
serverWriter.close();
连接断开();
}
捕获(IOE异常){
e、 printStackTrace();
返回“服务器被拒绝”;
}
返回“上传”;
}
这是我的代码片段,如果有任何帮助,将不胜感激
谢谢!
Suman我想这不起作用吗?是的,我得到了错误:java.io.FileNotFoundException:/var/www/working/jsonFile.txt:open failed:enoint(没有这样的文件或目录)