public class Download extends Activity {Button dlTxtButton = null;
Button dlMp3Button = null;
OnClickListener DownloadTxtListener = null;
OnClickListener DownloadMp3Listener = null; /** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
dlTxtButton = (Button)findViewById(R.id.dlTxtButton);
DownloadTxtListener = new OnClickListener(){
@Override
public void onClick(View v) {
HttpDownloader httpDownloader = new HttpDownloader();
String lrc = httpDownloader.downloadTxt("http://localhost:80/wang/mylove.txt");
System.out.println(lrc);
}
};
dlTxtButton.setOnClickListener(DownloadTxtListener);
dlMp3Button = (Button)findViewById(R.id.dlMp3Button);
DownloadMp3Listener = new OnClickListener(){
@Override
public void onClick(View v) {
HttpDownloader httpDownloader = new HttpDownloader();
int result = httpDownloader.downloadFile("http://localhost:80/wang/mylove.mp3","mylove/","mylove.mp3");
System.out.println(result);
}
};
dlMp3Button.setOnClickListener(DownloadMp3Listener);
}
}public class HttpDownloader {
private URL url = null;
/*****根据URL下载文件,前提是这个文件当中的内容是文本,函数返回的是文本文件的内容****/
/*1.创建一个URL对象
2.通过URL对象,创建一个HttpURLConnection对象
3.通过urlConn.getInputStream()方法得到InputStream输入流
4.从InputStream中读取数据
*/
public String downloadTxt(String urlStr){
StringBuffer sb = new StringBuffer();//创建一个具有16个字符缓冲区的空字符串
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()));
//urlConn.getInputStream()得到一个输入流,代表在这个地址下载的文本文件
while((line = buffer.readLine()) != null)
{
sb.append(line);//在空字符串后面追加字符串数据
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
buffer.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return sb.toString();
}/****创建任意形式的文件
*该函数返回整形
*返回-1,代表下载文件出错
*返回0,代表下载文件成功
*返回1,代表下载文件已存在
****/
public int downloadFile(String urlStr,String path,String fileName)//urlStr是传递网络中的地址
{
InputStream inputStream = null;
//创建FileUtils对象
try {
FileUtils fileUtils = new FileUtils();
if(fileUtils.isFileExit(path + fileName))
{
return 1;
}else{
inputStream = getInputStreamFromUrl(urlStr);
//调用writeToSDFromInput方法:创建一个目录,并且在这个目录下创建文件
File resultFile = fileUtils.writeToSDFromInput(path, fileName, inputStream);
if(resultFile == null){
return -1;
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return -1;
}finally{
try {
inputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return 0;
}
/*****根据URL得到输入流*****/
private InputStream getInputStreamFromUrl(String urlStr)
throws IOException {
url = new URL(urlStr);
HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
InputStream inputStream = urlConn.getInputStream();
return inputStream;
}
}public class FileUtils {
private String SDPATH;public String getSDPath()
{
return SDPATH;
}
public FileUtils()
{
//得到当前外部存储设备的目录
SDPATH = Environment.getExternalStorageState() + "/";
}/****在SD卡上创建文件 ****/public File createSDFile(String fileName) throws IOException
{
File file = new File(SDPATH + fileName);
file.createNewFile();//新建文件
return file;
}/****在SD卡上创建目录****/
public File creatSDDir(String dirName){
File dir = new File(SDPATH + dirName);
dir.mkdir();//新建目录
return dir;
}/****判断文件是否存在****/
public boolean isFileExit(String fileName){
File file = new File(SDPATH + fileName);
return file.exists();//判断文件或目录是否存在
}/****将一个InputStream里面的数据写到SD卡中****/
public File writeToSDFromInput(String path,String fileName,InputStream input){
File file = null;
FileOutputStream fos = null;
try {
creatSDDir(path);//创建一个目录
file = createSDFile(path + fileName);//在这个目录下创建文件
fos = new FileOutputStream(file);
byte buffer[] = new byte[4*1024];
while((input.read(buffer)) != -1)
{
fos.write(buffer);
}
fos.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return file;
}
}<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cn.hktk.wang"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".Download"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="4" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
</manifest>
04-07 14:58:21.995: ERROR/AndroidRuntime(732): at cn.hktk.wang.HttpDownloader.downloadTxt(HttpDownloader.java:40)
04-07 14:58:21.995: ERROR/AndroidRuntime(732): at cn.hktk.wang.Download$1.onClick(Download.java:27)
04-07 14:59:04.035: ERROR/AndroidRuntime(760): at cn.hktk.wang.HttpDownloader.downloadFile(HttpDownloader.java:79)
04-07 14:59:04.035: ERROR/AndroidRuntime(760): at cn.hktk.wang.Download$2.onClick(Download.java:39)
它出现了空指针异常,但是我找不出那里错误,我也在tomcat下部署了文件啊,怎么会出错呢?求大家帮帮忙,解决我的问题,谢谢大家
Button dlMp3Button = null;
OnClickListener DownloadTxtListener = null;
OnClickListener DownloadMp3Listener = null; /** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
dlTxtButton = (Button)findViewById(R.id.dlTxtButton);
DownloadTxtListener = new OnClickListener(){
@Override
public void onClick(View v) {
HttpDownloader httpDownloader = new HttpDownloader();
String lrc = httpDownloader.downloadTxt("http://localhost:80/wang/mylove.txt");
System.out.println(lrc);
}
};
dlTxtButton.setOnClickListener(DownloadTxtListener);
dlMp3Button = (Button)findViewById(R.id.dlMp3Button);
DownloadMp3Listener = new OnClickListener(){
@Override
public void onClick(View v) {
HttpDownloader httpDownloader = new HttpDownloader();
int result = httpDownloader.downloadFile("http://localhost:80/wang/mylove.mp3","mylove/","mylove.mp3");
System.out.println(result);
}
};
dlMp3Button.setOnClickListener(DownloadMp3Listener);
}
}public class HttpDownloader {
private URL url = null;
/*****根据URL下载文件,前提是这个文件当中的内容是文本,函数返回的是文本文件的内容****/
/*1.创建一个URL对象
2.通过URL对象,创建一个HttpURLConnection对象
3.通过urlConn.getInputStream()方法得到InputStream输入流
4.从InputStream中读取数据
*/
public String downloadTxt(String urlStr){
StringBuffer sb = new StringBuffer();//创建一个具有16个字符缓冲区的空字符串
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()));
//urlConn.getInputStream()得到一个输入流,代表在这个地址下载的文本文件
while((line = buffer.readLine()) != null)
{
sb.append(line);//在空字符串后面追加字符串数据
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
buffer.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return sb.toString();
}/****创建任意形式的文件
*该函数返回整形
*返回-1,代表下载文件出错
*返回0,代表下载文件成功
*返回1,代表下载文件已存在
****/
public int downloadFile(String urlStr,String path,String fileName)//urlStr是传递网络中的地址
{
InputStream inputStream = null;
//创建FileUtils对象
try {
FileUtils fileUtils = new FileUtils();
if(fileUtils.isFileExit(path + fileName))
{
return 1;
}else{
inputStream = getInputStreamFromUrl(urlStr);
//调用writeToSDFromInput方法:创建一个目录,并且在这个目录下创建文件
File resultFile = fileUtils.writeToSDFromInput(path, fileName, inputStream);
if(resultFile == null){
return -1;
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return -1;
}finally{
try {
inputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return 0;
}
/*****根据URL得到输入流*****/
private InputStream getInputStreamFromUrl(String urlStr)
throws IOException {
url = new URL(urlStr);
HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
InputStream inputStream = urlConn.getInputStream();
return inputStream;
}
}public class FileUtils {
private String SDPATH;public String getSDPath()
{
return SDPATH;
}
public FileUtils()
{
//得到当前外部存储设备的目录
SDPATH = Environment.getExternalStorageState() + "/";
}/****在SD卡上创建文件 ****/public File createSDFile(String fileName) throws IOException
{
File file = new File(SDPATH + fileName);
file.createNewFile();//新建文件
return file;
}/****在SD卡上创建目录****/
public File creatSDDir(String dirName){
File dir = new File(SDPATH + dirName);
dir.mkdir();//新建目录
return dir;
}/****判断文件是否存在****/
public boolean isFileExit(String fileName){
File file = new File(SDPATH + fileName);
return file.exists();//判断文件或目录是否存在
}/****将一个InputStream里面的数据写到SD卡中****/
public File writeToSDFromInput(String path,String fileName,InputStream input){
File file = null;
FileOutputStream fos = null;
try {
creatSDDir(path);//创建一个目录
file = createSDFile(path + fileName);//在这个目录下创建文件
fos = new FileOutputStream(file);
byte buffer[] = new byte[4*1024];
while((input.read(buffer)) != -1)
{
fos.write(buffer);
}
fos.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return file;
}
}<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cn.hktk.wang"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".Download"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="4" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
</manifest>
04-07 14:58:21.995: ERROR/AndroidRuntime(732): at cn.hktk.wang.HttpDownloader.downloadTxt(HttpDownloader.java:40)
04-07 14:58:21.995: ERROR/AndroidRuntime(732): at cn.hktk.wang.Download$1.onClick(Download.java:27)
04-07 14:59:04.035: ERROR/AndroidRuntime(760): at cn.hktk.wang.HttpDownloader.downloadFile(HttpDownloader.java:79)
04-07 14:59:04.035: ERROR/AndroidRuntime(760): at cn.hktk.wang.Download$2.onClick(Download.java:39)
它出现了空指针异常,但是我找不出那里错误,我也在tomcat下部署了文件啊,怎么会出错呢?求大家帮帮忙,解决我的问题,谢谢大家
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货