java 弹出另存为_java如何实现 io流传输过来的文件,提示另存为弹出窗口?

java如何实现 io流传输过来的文件,提示另存为弹出窗口?

mip版  关注:166  答案:6  悬赏:0

解决时间 2021-01-24 22:00

3bf47ef4f73d462abb84057dfcdc0a3b.png

已解决

2021-01-24 11:06

最佳答案

c347a8640a7d548aed4aac5e529823ea.png

2021-01-24 11:35

弹出窗口,我理解为浏览器弹出窗口,所以必定有后端服务器程序,这里重点说的就是服务器程序。

第一步:设置Response头部(最关键)

response.setContentType("application/octet-stream;charset=UTF-8");

// 设置弹出框提示的文件名

response.addHeader("Content-Disposition", "attachment; filename=" + java.net.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();

全部回答

88b7e031db2027cb83a9fc657aa1bc42.png

1楼

2021-01-24 16:38

web开发吗。如果是java的web开发。

response.setHeader("Content-disposition", "attachment; filename=aaa.xls");// 设定输出文件头

response.setContentType("ContentType");// 定义输出类型注意,这里的文件名需要用iso8859-1编码

31fe5e7404978d3426e0a6390c4fa644.png

2楼

2021-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;

}

25ba955bbf33635362ba703d7a3921d1.png

3楼

2021-01-24 14:51

private void downValid(HttpServletResponse response,NetDiskFile netDiskFile)throws Exception{
try{
if(netDiskFile!=null){
File f = new File(netDiskFile.getAttach());
//文件流的输入
BufferedInputStream br = new BufferedInputStream(new FileInputStream(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 = new byte[1024];
int len = 0;
//文件流的输出
OutputStream output = response.getOutputStream();
while ((len = br.read(buf)) > 0){
output.write(buf, 0, len);
}
br.close();
output.close();
}else{
PrintWriter out=response.getWriter();
out.println("<script language='javascript'>alert(\"you only can download the file, can't do the folder!\");history.back();</script>");
}
}catch(FileNotFoundException e){
PrintWriter out=response.getWriter();
out.print("<script language='javascript'>alert('Sorry,the file could not be found');history.back();</script>");
}catch(Exception e){
PrintWriter out=response.getWriter();
out.print("<script language='javascript'>alert('while downloading,the error happens.');history.back();</script>");
}
}
手写不容易,望采纳,万分感激。

93db2c08f70ba283a7bb6fb7059f45cf.png

4楼

2021-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流会一直存在,直到程序运行结束。

f95c91d2c0f40d08ad422a6905e88867.png

5楼

2021-01-24 12:51

1.通过链接src直接文件路劲。

2.点击一个链接或者按钮,请求一个action或者serlvet。

response.setcontenttype("application/octet-stream");

通过response.getoutputstream();获取输出流,然后write写数据就行了。

我要举报

如果感觉以上信息为低俗/不良/侵权的信息,可以点下面链接进行举报,我们会做出相应处理,感谢你的支持!

点此我要举报以上信息!

推荐资讯

大家都在看

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值