Android开发之下载Tomcat服务器的文件到模拟器的SD卡

时间:2021-08-18 05:03:54

Tomcat服务器可以到Apache的官网去下载http://tomcat.apache.org/,如何配置和使用百度下也有很多介绍,只要把Java的SDK配下java_home环境变量就行了,因为Tomcat也是用Java程序写的所以需要依赖Java的库。如果配置成功在IE上输入:http://localhost:8080/回车就会出现Tomcat的主页

Android开发之下载Tomcat服务器的文件到模拟器的SD卡

对SD卡进行操作需要新建一个工具类可以写入字节也可以写入字符  FileUitls.java

package com.example.utils;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream; import android.os.Environment; /**
* 文件管理类 在以后的开发中也可以使用这个工具类,提高代码的利用性
* 只要是对SD卡的操作
* 1、获取SD卡路径 getSDPATH
* 2、在SD卡上根据传入的目录名创建目录 createSDDir
* 3、在创建上目录后可以在该目录上创建文件 createSDFile
* 4、检测文件是否存在 isFileExist
* 5、将一个InputStream写入到SD卡中 write2SDFromInput
* 6、将一个字符流写入到SD卡 write2SDFromWrite
* 注:如果要写入SD卡,只要调用write2SDFromInput函数即可
* @author Administrator
*
*/
public class FileUitls {
private static String SDPATH; public FileUitls(){
//得到当前设备外部存储设备的目录
SDPATH = Environment.getExternalStorageDirectory() + File.separator;
}
/**
* 获取当前SD卡的根目录
* @return
*/
public String getSDPATH(){
return SDPATH;
}
/**
* SD卡上创建目录
*/
public File createSDDir(String dirName){
File dir = new File(SDPATH + dirName);
System.out.println("createSDDir " + SDPATH + dirName);
dir.mkdir();
return dir;
}
/**
* SD卡上创建文件
*/
public File createSDFile(String fileName)throws IOException{
File file = new File(SDPATH + fileName);
System.out.println("createSDFile " + SDPATH + fileName);
file.createNewFile();
return file;
} /**
* 判断SD卡上的文件是否存在
*/
public boolean isFileExist(String fileName){
File file = new File(SDPATH + fileName);
return file.exists();
}
/**
* 将一个InputStream字节流写入到SD卡中
*/
public File write2SDFromInput(String Path, String FileName, InputStream input){
File file = null;
OutputStream output = null; //创建一个写入字节流对象
try{
createSDDir(Path); //根据传入的路径创建目录
file = createSDFile(Path + FileName); //根据传入的文件名创建
output = new FileOutputStream(file);
byte buffer[] = new byte[4 * 1024]; //每次读取4K
int num = 0; //需要根据读取的字节大小写入文件
while((num = (input.read(buffer))) != -1){
output.write(buffer, 0, num);
}
output.flush(); //清空缓存
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
finally{
try{
output.close();
}
catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
return file;
} /**
* 把传入的字符流写入到SD卡中
* @param Path
* @param FileName
* @param input
* @return
*/
public File write2SDFromWrite(String Path, String FileName, BufferedReader input){
File file = null;
FileWriter output = null; //创建一个写入字符流对象
BufferedWriter bufw = null;
try{
createSDDir(Path); //根据传入的路径创建目录
file = createSDFile(Path + FileName); //根据传入的文件名创建
output = new FileWriter(file);
bufw = new BufferedWriter(output);
String line = null;
while((line = (input.readLine())) != null){
System.out.println("line = " + line);
bufw.write(line);
bufw.newLine();
}
bufw.flush(); //清空缓存
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
finally{
try{
bufw.close();
}
catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
return file;
}
}

还需要创建一个下载工具类  HttpDownloader.java

对于下载类,要注意的就是下载文件变成乱码问题,如果有中文最好指定gb3212编码或UTF-8

package com.example.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL; public class HttpDownloader {
private URL url = null; /**
* 根据URL下载文件,前提是这个文件当中的内容是文本,函数的返回值就是文件当中的内容
* 1.创建一个URL对象
* 2.通过URL对象,创建一个HttpURLConnection对象
* 3.得到InputStram
* 4.从InputStream当中读取数据
*
* @param urlStr
* @return
*/
public String download(String urlstr) {
StringBuffer sb = new StringBuffer();
String line = null;
BufferedReader buffer = null;
try {
// 创建一个URL对象
url = new URL(urlstr);
// 创建一个Http连接
HttpURLConnection urlConn = (HttpURLConnection) url
.openConnection();
// 使用IO流读取数据
buffer = new BufferedReader(new InputStreamReader(
urlConn.getInputStream(), "gb2312")); // 防止中文出现乱码 gb2312
while ((line = buffer.readLine()) != null) {
sb.append(line);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
buffer.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return sb.toString();
} /**
* 可以下载字节流文件到SD卡中
*
* @param urlstr 要下载文件的URI地址
* @param Path 在SD卡上文件夹的路径
* @param FileName 在SD卡上文件的名称
* @return 该函数返回整型:-1代表下载失败,0代表下载成功,1代表文件已经存在
*/
public int download(String urlstr, String Path, String FileName) {
InputStream inputstream = null;
BufferedReader buffer = null;
try {
FileUitls fileUitls = new FileUitls();
System.out.println(Path + FileName);
if (fileUitls.isFileExist(Path + FileName)) {
return 1;
} else {
// 获取URI中的字节流
inputstream = getInputStreamFromUrl(urlstr);
// 把字节流转换成字符流
buffer = new BufferedReader(new InputStreamReader(inputstream,
"gb2312")); // 防止中文出现乱码 UTF-8
File resultFile = fileUitls.write2SDFromWrite(Path, FileName,
buffer);
if (resultFile == null) {
return -1;
}
} } catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
return -1;
} finally {
try {
if(buffer != null)
buffer.close();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
return 0;
} /**
* 可以下载字符流和字节流文件到SD卡中
*
* @param urlstr
* @param Path
* @param FileName
* @return 该函数返回整型:-1代表下载失败,0代表下载成功,1代表文件已经存在
*/
public int downFile(String urlstr, String Path, String FileName) {
InputStream inputstream = null;
try {
FileUitls fileUitls = new FileUitls();
if (fileUitls.isFileExist(Path + FileName)) {
return 1;
} else {
inputstream = getInputStreamFromUrl(urlstr);
File resultFile = fileUitls.write2SDFromInput(Path, FileName,
inputstream);
if (resultFile == null) {
return -1;
}
} } catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
return -1;
} finally {
try {
inputstream.close();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
return 0;
} /**
* 根据URL得到输入流
*
* @param urlstr
* @return
* @throws MalformedURLException
* @throws IOException
*/
private InputStream getInputStreamFromUrl(String urlstr)
throws MalformedURLException, IOException {
// TODO Auto-generated method stub
url = new URL(urlstr);
HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
InputStream inputStream = urlConn.getInputStream();
return inputStream;
}
}

在主界面上有添加两个按钮,分别下载歌词和歌曲

package com.example.download;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button; import com.example.utils.HttpDownloader; public class Download extends Activity { private Button downloadtxt;
private Button downloadmp3; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
downloadtxt = (Button) findViewById(R.id.download_txt);
downloadtxt.setOnClickListener(new DownLoadTxt());
downloadmp3 = (Button) findViewById(R.id.download_mp3);
downloadmp3.setOnClickListener(new DownLoadMp3());
} /**
* 下载文本文件,主要是字符流
* @author Administrator
*
*/
class DownLoadTxt implements OnClickListener { @Override
public void onClick(View v) { // TODO Auto-generated method stub
HttpDownloader httpDownloader = new HttpDownloader();
int result = httpDownloader.download(
"http://192.168.0.152:8080/mp3/a1.lrc", "mp3Test/abc/",
"a1.lrc");
System.out.println("down load txt result = " + result);
// String lrc =
// httpDownloader.download("http://192.168.0.152:8080/mp3/a1.lrc");
// System.out.println("down load txt = " + lrc);
} } /**
* 下载字节流文件,也可以下载字符流文件
* @author Administrator
*
*/
class DownLoadMp3 implements OnClickListener { @Override
public void onClick(View v) {
// TODO Auto-generated method stub
HttpDownloader httpDownloader = new HttpDownloader();
int result = httpDownloader.downFile(
"http://192.168.0.152:8080/mp3/a1.mp3", "mp3Test/",
"a1.mp3");
System.out.println("downfile load txt result = " + result);
}
}
}

其中192.168.0.152是本地IP地址8080是Tomcat的端口号

下载后的两个文件如图

Android开发之下载Tomcat服务器的文件到模拟器的SD卡

样例代码

点击打开链接