开发者

How to upload .txt file to http server?

I want to upload a .txt开发者_JS百科 file(only) from a device to my server. How can I do this? I also want to be able to download another .txt file from the server to a device.

Any ideas where to start?

Thanks..


Use HttpClient and HttpPost from the HttpComponents library available for java to post a file to your server via http. You can use the MultipartEntity and/or FileEntity class for representing your file data.

see example here, or see multipart example below:

HttpClient httpclient = new DefaultHttpClient();
httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);

HttpPost httppost = new HttpPost(url);     

// add file content and metadata
MultipartEntity mpEntity = new MultipartEntity();
ContentBody cbFile = new FileBody(targetFile, "image/jpeg");
mpEntity.addPart("userfile", cbFile);
mpEntity.addPart( "commentText", new StringBody(commentText, "text/plain",
                Charset.forName( "UTF-8" )));

httppost.setEntity(mpEntity);

HttpResponse response = httpclient.execute(httppost);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜