上传文件的网址是什么? [英] Upload a file to an URL?
本文介绍了上传文件的网址是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要修改我的Java类,让我成功地上传一个文件中的URL。
我想使用的网络浏览器内置的文件上传机制。
这是我迄今所做的: -
进口android.os.Bundle;
进口android.app.Activity;
进口android.view.Menu;
进口android.webkit.WebView;
公共类MainActivity延伸活动{
的WebView网页;
@覆盖
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
网页=(web视图)findViewById(R.id.web);
web.getSettings()setJavaScriptEnabled(真)。
web.loadUrl(http://encodable.com/uploaddemo/);
}
@覆盖
公共布尔onCreateOptionsMenu(功能菜单){
//充气菜单;这增加了项目操作栏,如果它是present。
。getMenuInflater()膨胀(R.menu.main,菜单);
返回true;
}
}
解决方案
使用这个类来上传文件...希望这可能对你有帮助。
公共类TryFile {
公共静态无效的主要(字串[] AR)
抛出HttpException,IOException异常,的URISyntaxException {
TryFile T =新TryFile();
t.method();
}
公共无效方法()抛出HttpException,IOException异常,的URISyntaxException {
字符串URL =http://encodable.com/uploaddemo/;
字符串文件名=; //要上传的文件名
HttpClient的HttpClient的=新DefaultHttpClient();
HttpPost httppost =新HttpPost(URL);
FileBody fileContent =新FiSystem.out.println(你好);
StringBody评论=新StringBody(文件名:+文件名);
MultipartEntity reqEntity =新MultipartEntity();
reqEntity.addPart(文件,fileContent);
httppost.setEntity(reqEntity);
HTT presponse响应= httpclient.execute(httppost);
HttpEntity resEntity = response.getEntity();
}
}
I need to modify my java Class to allow me to successfully upload a file to the URL within.
I want to use the web browser's built-in file upload mechanism.
This is what I have done so far:-
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.webkit.WebView;
public class MainActivity extends Activity {
WebView web;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
web=(WebView)findViewById(R.id.web);
web.getSettings().setJavaScriptEnabled(true);
web.loadUrl("http://encodable.com/uploaddemo/");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
解决方案
Use this class to upload your file...hope this might be helpful to you
public class TryFile {
public static void main(String[] ar)
throws HttpException, IOException, URISyntaxException {
TryFile t = new TryFile();
t.method();
}
public void method() throws HttpException, IOException, URISyntaxException {
String url = "http://encodable.com/uploaddemo/";
String fileName = ""; //file name to be uploaded
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
FileBody fileContent = new FiSystem.out.println("hello");
StringBody comment = new StringBody("Filename: " + fileName);
MultipartEntity reqEntity = new MultipartEntity();
reqEntity.addPart("file", fileContent);
httppost.setEntity(reqEntity);
HttpResponse response = httpclient.execute(httppost);
HttpEntity resEntity = response.getEntity();
}
}
这篇关于上传文件的网址是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文