我想将图像转换为byte[]
并通过servlet发送。
我已将图像转换为byte[]
但将其发送给客户端??
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String name=request.getParameter("name");
File f = new File("/Users/shilu/MyProject/Chat/Photo/" + name);
byte[] data = Files.readAllBytes(f.toPath());
//What to do now??
}
答案 0 :(得分:1)
这取决于您需要指定与您的图片类型匹配的content type的图片类型。
所以这里假设您要发送png
,代码可能是这样的:
// Set the media type matching with my image
response.setContentType("image/png");
// Write the data
try (OutputStream out = response.getOutputStream()) {
out.write(data);
}