java用流的方式加载图片

时间:2021-05-07 04:16:31

前端:

<img ng-show="photo" class="vertical_b" id="photoImg" src="http://localhost:9999/manage-admin/filemanager/prevViewByPath.action?filePath=cf532711-6ba8-4259-8c30-1dbb13fe3a82.jpg">

后端:

/**
* @throws IOException
* @throws FileNotFoundException
* 功能:预览
* @param
* @return
* @throws
*/
public String prevViewByPath() throws IOException{
String catalinaHome = filePathConfig.getFileUploadPath();
String targetDirectory = catalinaHome + "/upload/";
String filedownload = targetDirectory + filePath;
File f = new File(filedownload);
if(!f.exists()){
return null;
}
FileInputStream fis = new FileInputStream(f);
OutputStream out = getResponse().getOutputStream();
byte[] bs = new byte[1024];
while ((fis.read(bs) != -1)) {
out.write(bs);
}
out.flush();
out.close();
out = null;
fis.close();
return null;
}