android从服务器上下载文件详解Word格式文档下载.docx
- 文档编号:21656309
- 上传时间:2023-01-31
- 格式:DOCX
- 页数:17
- 大小:180.32KB
android从服务器上下载文件详解Word格式文档下载.docx
《android从服务器上下载文件详解Word格式文档下载.docx》由会员分享,可在线阅读,更多相关《android从服务器上下载文件详解Word格式文档下载.docx(17页珍藏版)》请在冰豆网上搜索。
returnnameString;
*@paramnameStringthenameStringtoset
publicvoidsetNameString(StringnameString){
this.nameString=nameString;
/*(non-Javadoc)
*@seejava.lang.Object#toString()
@Override
publicStringtoString(){
return"
info[id="
+id+"
nameString="
+nameString+"
]"
;
infoHandle.java
importjava.util.List;
importorg.xml.sax.Attributes;
importorg.xml.sax.SAXException;
importorg.xml.sax.helpers.DefaultHandler;
importAndroid.util.Log;
publicclassinfoHandleextendsDefaultHandler{
privateInfoMp3_info;
privateList<
Info>
List_Mp3_Infos;
privateStringpreString;
publicinfoHandle(){
super();
}
publicinfoHandle(List<
list_Mp3_Infos){
List_Mp3_Infos=list_Mp3_Infos;
@Override
publicvoidstartDocument()throwsSAXException{
//TODOAuto-generatedmethodstub
super.startDocument();
publicvoidstartElement(Stringuri,StringlocalName,StringqName,
Attributesattributes)throwsSAXException{
preString=localName;
if(preString.equals("
resource"
)){
Mp3_info=newInfo();
super.startElement(uri,localName,qName,attributes);
publicvoidendElement(Stringuri,StringlocalName,StringqName)
throwsSAXException{
preString="
"
Log.i("
endElement_localName-------->
"
localName);
if(localName.equals("
List_Mp3_Infos.add(Mp3_info);
super.endElement(uri,localName,qName);
publicvoidcharacters(char[]ch,intstart,intlength)
StringdateString;
dateString=newString(ch,start,length);
id"
Mp3_info.setId(Integer.parseInt(dateString));
id--------->
dateString);
}elseif(preString.equals("
mp3.name"
Mp3_info.setNameString(dateString);
mp3.name--------->
super.characters(ch,start,length);
publicvoidendDocument()throwsSAXException{
super.endDocument();
DonwnFileActivity.java
importjava.io.File;
importjava.io.FileOutputStream;
importjava.io.IOException;
importjava.io.InputStream;
importjava.io.OutputStream;
import.HttpURLConnection;
import.MalformedURLException;
import.URL;
importjava.util.ArrayList;
importjava.util.Iterator;
importjavax.xml.parsers.ParserConfigurationException;
importjavax.xml.parsers.SAXParser;
importjavax.xml.parsers.SAXParserFactory;
importorg.apache.http.HttpConnection;
importorg.apache.http.message.BufferedHeader;
importorg.xml.sax.InputSource;
importorg.xml.sax.XMLReader;
importorg.xmlpull.v1.XmlPullParser;
importorg.xmlpull.v1.XmlPullParserException;
importorg.xmlpull.v1.XmlPullParserFactory;
importandroid.app.Activity;
importandroid.app.ProgressDialog;
importandroid.content.Intent;
importandroid.content.res.AssetManager.AssetInputStream;
importandroid.os.Bundle;
importandroid.os.Environment;
importandroid.util.Log;
importandroid.view.MenuItem;
importandroid.view.View;
importandroid.view.View.OnClickListener;
importandroid.widget.AdapterView;
importandroid.widget.AdapterView.OnItemClickListener;
importandroid.widget.ArrayAdapter;
importandroid.widget.Button;
importandroid.widget.ListAdapter;
importandroid.widget.ListView;
importandroid.widget.SimpleAdapter;
importandroid.widget.TextView;
importandroid.widget.Toast;
publicclassDownFileActivityextendsActivity{
/**Calledwhentheactivityisfirstcreated.*/
privateButtonbutton;
privateListViewlistView;
String>
list=newArrayList<
();
privateArrayAdapter<
adapter1;
musicnameList;
privateIntentservice;
publicvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
service=newIntent(this,DownService.class);
button=(Button)findViewById(R.id.button1);
listView=(ListView)findViewById(R.id.listView1);
button.setOnClickListener(newMybutton());
classMybuttonimplementsOnClickListener{
publicvoidonClick(Viewv){
button.setText("
刷新列表"
);
musicnameList=newArrayList<
List<
lInfos=parseXML();
list.clear();
adapter1=newArrayAdapter<
(getApplicationContext(),android.R.layout.simple_list_item_1,list);
listView.setAdapter(adapter1);
for(Iteratoriterator=lInfos.iterator();
iterator.hasNext();
){
Infoinfo=(Info)iterator.next();
musicnameList.add(info.getNameString());
list.add(info.getId()+"
+info.getNameString());
listView.setOnItemClickListener(newItemLitener());
parseXML()
{
infos=null;
SAXParserFactoryfactory=SAXParserFactory.newInstance();
try{
URLurl=newURL("
http:
//210.30.12.34:
8080/mp3/resources.xml"
HttpURLConnectionconnection=(HttpURLConnection)url.openConnection();
if(connection.getReadTimeout()==4){
Toast.makeText(getApplicationContext(),"
请求超时"
Toast.LENGTH_LONG).show();
returnnull;
InputStreaminputStream2=connection.getInputStream();
SAXParserparser=factory.newSAXParser();
XMLReaderreader=parser.getXMLReader();
infos=newArrayList<
reader.setContentHandler(newinfoHandle(infos));
reader.parse(newInputSource(inputStream2));
}catch(ParserConfigurationExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}catch(SAXExceptione){
}catch(MalformedURLExceptione){
}catch(IOExceptione){
returninfos;
privateclassItemLitenerimplementsOnItemClickListener{
publicvoidonItemClick(AdapterView<
?
>
arg0,Viewarg1,intarg2,longarg3){
Toast.makeText(getApplicationContext(),musicnameList.get(arg2).toString(),Toast.LENGTH_LONG).show();
StringdownUrlString="
8080/mp3/"
+musicnameList.get(arg2).toString();
StringsdcardSave="
DownFile"
Stringfilename=musicnameList.get(arg2).toString();
OutputStreamoutputStream=null;
HttpURLConnectionconnection;
/*
*1.在AndroidMainfest.xml中进行权限配置
*<
uses-permissionandroid:
name="
android.permission.WRITE_EXTERNAL_STORAGE"
/>
*取得写入SDCard的权限
*2.取得SDCard的路径:
Environment.getExternalStorageDirectory()
*3.检查要保存的文件上是否已经存在
*4.不存在,新建文件夹,新建文件
*5.将input流中的信息写入SDCard
*6.关闭流
StringSDcardPath=Environment.getExternalStorageDirectory()+"
StringpathString=SDcardPath+"
/"
+sdcardSave+"
+filename;
Filefile=newFile(pathString);
startService(service);
connection=(HttpURLConnection)newURL(downUrlString).openConnection();
InputStreaminputStream=connection.getInputStream();
if(!
file.exists()){
newFile(SDcardPath+"
+sdcardSave).mkdir();
File[]filelist=newFile(SDcardPath+"
+sdcardSave).listFiles();
for(inti=0;
i<
filelist.length;
i++){
if(filelist[i].getName().equals(filename)){
Toast.makeText(getApplicationContext(),filename+"
已经存在"
return;
file.createNewFile();
System.err.println("
开始下载"
outputStream=newFileOutputStream(file);
byte[]buffer=newbyte[4*1024];
while((inputStream.read(buffer))!
=-1){
outputStream.write(buffer);
outputStream.flush();
}finally{
outputStream.close();
下载成功"
下载完成"
//progressDialog.dismiss();
}catch(Exceptione2){
//TODO:
handleexception
下载失败"
//progressDialog.cancel();
DownService.java
importAndroid.app.Service;
importandroid.os.IBinder;
publicclassDownServiceextendsService{
publicIBinderonBind(Intentarg0){
publicvoidonCreate(){
super.onCreate();
publicvoidonDestroy(){
super.onDestroy();
publicvoidonStart(Intentintent,intstartId){
Toast.makeText(getApplicationContext
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- android 服务器 上下 载文 详解