本地图片Base64编码及文件转流

编程

/**

* 本地图片base64编码" title="图片base64编码">图片base64编码

*/

public static String base64Img(String path) {

InputStream is = null;

ByteArrayOutputStream outStream = null;

try {

is = new FileInputStream(path);

outStream = new ByteArrayOutputStream();

byte[] buffer = new byte[1024];

int len = 0;

while ((len = is.read(buffer)) != -1) {

outStream.write(buffer, 0, len);

}

is.close();

} catch (IOException e) {

e.printStackTrace();

}

return new BASE64Encoder().encode(outStream.toByteArray())

.replace("data/image/jpeg/base64/", "");

}

/**

* 本地文件以流方式处理

*/

public static void downloadByStream(HttpServletResponse response, HttpServletRequest request,

String fileName, String filePath) {

byte[] buffer = new byte[1024];

FileInputStream fis = null;

BufferedInputStream bis = null;

try {

/* 解决中文文件名称的问题 */

File file = ResourceUtils.getFile(filePath);

String filenameIso = encodeName(request, fileName);

/* 设置文件名 */

response.addHeader("Content-Disposition", "attachment;fileName=" + filenameIso);

fis = new FileInputStream(file);

bis = new BufferedInputStream(fis);

OutputStream os = response.getOutputStream();

int i = bis.read(buffer);

while (i != -1) {

os.write(buffer, 0, i);

i = bis.read(buffer);

}

} catch (Exception e) {

throw new AppException(ErrorCode.UNEXCEPTED, e.getMessage());

} finally {

/* 关闭流 */

IOUtils.closeQuietly(bis);

IOUtils.closeQuietly(fis);

}

}

以上是 本地图片Base64编码及文件转流 的全部内容, 来源链接: utcz.com/z/510959.html

回到顶部