URL ulr = new URL(....)
URLConnection urlConnection = url.openConnection();
InputStream in = urlConnection .getInputStream()
OutputStream output = urlConnection .getOutputStream()
URLConnection urlConnection = url.openConnection();
InputStream in = urlConnection .getInputStream()
OutputStream output = urlConnection .getOutputStream()
{
InputStream filecon = null;
DataInputStream filedata = null;
int filelength;
URL fileur;
try
{
fileur = new URL(strurl);
URLConnection uc = fileur.openConnection();
filelength=uc.getContentLength();
filecon = fileur.openStream();
filedata = new DataInputStream(filecon);
byte temp[]=new byte[filelength];
filedata.read(temp);
return temp;
}
catch(Exception e)
{
return null;
}
}看看这样行吗?
{
InputStream in= null;
ByteArrayOutputStream out=new ByteArrayOutputStream();
byte temp[]=new byte[1024];
URL url=null;
try
{
url= new URL(strurl);
in= url.openStream();
int len=0;
while((len=in.read(temp))!=-1){
out.write(temp,0,len);
}
}
catch(Exception e)
{
}finally{
try{
if(in!=null)
in.close();
}catch(Exception e){
}
try{
if(out!=null)
out.close();
}catch(Exception e){ } }
return out.toByteArray();
}
另外文本文件和图像文件是一样的。因为读取的数据就是文件内容--包括具体内容和一些表示格式的信息
InputStream in= null;
m_file = new File(filename);
try{
FileOutputStream bos = new FileOutputStream(m_file);
if(!m_file.exists())
{
m_file.createNewFile();
}
byte[] buff = myUrl.ReadFile(strurl);
int bytesRead;
System.out.println("file length is "+buff.length);
while(-1 != (bytesRead = in.read(buff, 0,buff.length))) {
bos.write(buff, 0, buff.length);
}
}catch(Exception e){
//out.print("exception");
}
为什么产生的文件大小为零?????