java如何实现 io流传输过来的文件,提示另存为弹出窗口?
mip版关注:166答案:6悬赏:0
解决时间 -01-24 22:00
已解决
-01-24 11:06
最佳答案
-01-24 11:35
弹出窗口,我理解为浏览器弹出窗口,所以必定有后端服务器程序,这里重点说的就是服务器程序。
第一步:设置Response头部(最关键)
response.setContentType("application/octet-stream;charset=UTF-8");
// 设置弹出框提示的文件名
response.addHeader("Content-Disposition", "attachment; filename=" + .URLEncoder.encode(fileName, "UTF-8"));
第二步:解析输入流
// 这里的in为你的输入流
BufferedInputStream is = new BufferedInputStream(in);
// 准备缓冲区
byte[] buffer = new byte[4096];
第三步:将输入流转换为输出流
BufferedOutputStream os = new BufferedOutputStream(response.getOutputStream());
int offset = 0;
while((offset = is.read(buffer, 0, 4096) > -1) {
os.write(buffer, 0, offset)
}
第四步:关闭输入输出流
os.close();
is.close();
全部回答
1楼
-01-24 16:38
web开发吗。如果是java的web开发。
response.setHeader("Content-disposition","attachment;filename=aaa.xls");//设定输出文件头
response.setContentType("ContentType");//定义输出类型注意,这里的文件名需要用iso8859-1编码
2楼
-01-24 15:27
可以通过swing技术中的JFileChooser类来实现;
方法如下:
public File getFile(){
final JFileChooser fc = new JFileChooser();
fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
// JFileChooser.FILES_ONLY
// JFileChooser.DIRECTORIES_ONLY
int returnVal = fc.showOpenDialog(this);
File file_choosed = fc.getSelectedFile();
return file_choosed;
}
3楼
-01-24 14:51
privatevoiddownValid(HttpServletResponseresponse,NetDiskFilenetDiskFile)throwsException{
try{
if(netDiskFile!=null){
Filef=newFile(netDiskFile.getAttach());
//文件流的输入
BufferedInputStreambr=newBufferedInputStream(newFileInputStream(f));
response.reset();
response.setCharacterEncoding("gb2312");
response.setContentType("application/x-msdownload");
response.setHeader("Content-Disposition",
"attachment;filename="+this.toUtf8String(netDiskFile.getFilename())+"."+netDiskFile.getSuffix());
byte[]buf=newbyte[1024];
intlen=0;
//文件流的输出
OutputStreamoutput=response.getOutputStream();
while((len=br.read(buf))>0){
output.write(buf,0,len);
}
br.close();
output.close();
}else{
PrintWriterout=response.getWriter();
out.println("<scriptlanguage='javascript'>alert(\"youonlycandownloadthefile,can'tdothefolder!\");history.back();</script>");
}
}catch(FileNotFoundExceptione){
PrintWriterout=response.getWriter();
out.print("<scriptlanguage='javascript'>alert('Sorry,thefilecouldnotbefound');history.back();</script>");
}catch(Exceptione){
PrintWriterout=response.getWriter();
out.print("<scriptlanguage='javascript'>alert('whiledownloading,theerrorhappens.');history.back();</script>");
}
}
手写不容易,望采纳,万分感激。
4楼
-01-24 13:12
可以通过BufferedReader 流的形式进行流读取,之后通过readLine方法获取到读取的内容。
BufferedReader bre = null;
try {
String file = "D:/test/test.txt";
bre = new BufferedReader(new FileReader(file));//此时获取到的bre就是整个文件的缓存流
while ((str = bre.readLine())!= null) // 判断最后一行不存在,为空结束循环
{
System.out.println(str);//原样输出读到的内容
};
备注: 流用完之后必须close掉,如上面的就应该是:bre.close(),否则bre流会一直存在,直到程序运行结束。
5楼
-01-24 12:51
1.通过链接src直接文件路劲。
2.点击一个链接或者按钮,请求一个action或者serlvet。
response.setcontenttype("application/octet-stream");
通过response.getoutputstream();获取输出流,然后write写数据就行了。
我要举报
如果感觉以上信息为低俗/不良/侵权的信息,可以点下面链接进行举报,我们会做出相应处理,感谢你的支持!
点此我要举报以上信息!
推荐资讯
大家都在看