首页 > 编程知识 正文

springboot中文手册,基于springboot的项目

时间:2023-05-04 21:43:29 阅读:130191 作者:4204

1.FileServiceImpl.java

//*

@Override

公共void下载文件(字符串地址,http servlet响应) throws sqddm {

if(address.contains('% ' ) }{

try {

地址=URL decoder.decode (地址,' UTF-8 ';

} catch (unsupportedencodingexceptione ) {

thrownewcyexception;

}

}

try {

in=新文件输入(新文件)地址);

string[]dir=address.split('/';

string filename=dir [ dir.length-1 ];

//将响应类型设置为html,编码为utf-8,处理该页面文本显示的乱码

response.set content type (应用程序/octet-stream );

//hxdxb标头设置:最后一个参数是设置下载hxdxb名称

response.setheader (' content-disposition ',' attachment; filename=' fileName;

out=response.getOutputStream (;

读取hxdxb流

int len=0;

byte[] buffer=new byte[1024 * 10];

wile((len=in.read ) ) buffer )!=-1 ()

out.write (缓冲器,0,len );

}

out.flush (;

}catch(filenotfoundexceptione ) {

thrownewcyexception;

} finally {

response.flushBuffer (;

try {

out.close (;

in.close (;

}catch(nullpointerexceptione ) )

thrownewcyexception (' responsefilestreamstreamclose ) ) error : nullpointerexception ' e.tostring );

}catch(exceptione ) {

thrownewcyexception (' responsefilestreamstreamclose ) ) error:' e.toString );

}

}

}

2.FileController.java

@ request mapping (value='/downloadfile ',method=RequestMethod.GET ) )

@响应主体

@Validated

public void downloadfile (@ not blank @ request param ) value='address ' ) String address,http servlet response () )

throws sqddm {

fileservice.downloadfile (地址,响应;

}

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。