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 (地址,响应;
}