我有一个url,想把它转换成file类型的文件。
现在我通过url.openstream()来获得inputstream类型的数据,然后把inputstream写入磁盘然后返回file文件,代码如下:URL url=new URL("http://www.fjdjh.com/sqx/sqx1.xls");
File excel=file_put_contents("D:\\1.xls",url.openStream());
public static File file_put_contents(String savePath,InputStream is){
File file=new File(savePath);
OutputStream os=null;
try{
os=new FileOutputStream(file);
byte buffer[]=new byte[4*1024];
int len=0;
while((len=is.read(buffer))!=-1){
os.write(buffer,0,len);
}
os.flush();
}
catch(Exception e){
e.printStackTrace();
}
finally{
try{
os.close();
}
catch(Exception e){
e.printStackTrace();
}
}
return file;
}
请问我不想再磁盘上新增文件,要怎么改写这个代码?
现在我通过url.openstream()来获得inputstream类型的数据,然后把inputstream写入磁盘然后返回file文件,代码如下:URL url=new URL("http://www.fjdjh.com/sqx/sqx1.xls");
File excel=file_put_contents("D:\\1.xls",url.openStream());
public static File file_put_contents(String savePath,InputStream is){
File file=new File(savePath);
OutputStream os=null;
try{
os=new FileOutputStream(file);
byte buffer[]=new byte[4*1024];
int len=0;
while((len=is.read(buffer))!=-1){
os.write(buffer,0,len);
}
os.flush();
}
catch(Exception e){
e.printStackTrace();
}
finally{
try{
os.close();
}
catch(Exception e){
e.printStackTrace();
}
}
return file;
}
请问我不想再磁盘上新增文件,要怎么改写这个代码?
解决方案 »
- 用org.dom4j.io.SAXValidator检验xml格式时并发报错,求教
- WebSphere Enterprise Service Bus FVT 是什么呢?
- Java Web 程序员工作内容和工作流程以及薪金待遇
- java 热键问题!
- Struts 2 带参数的 国际化 i18n
- 菜鸟的问题;JSP挺怪的现象
- 希望更多的朋友加入反垃圾邮件网格开发
- 谁用JBOSS成功配置过MySQL,请帮帮我,我快疯了!
- 有关struts的问题!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
- 500分求j2ee详细设计方案
- Spring MVC配置IOC 空指针的问题,围观啊,求解啊~!
- 求助hibernate 登陆
如果是的话直接用ByteArrayOutputStream字节数组流放在内存中
我是想这个过程在内存中进行,如果是用ByteArrayOutputStream,放在内存里的还是数据流。
我现在的问题是没法把inputstream(或者是URL)直接转换成File,不得已我用了writer往磁盘上面写了一下,然后才能获得磁盘上的File对象