Java 使用spring和apache common from文件夹在web上显示图像
在我的应用程序中,我有一个上传功能。为此,我使用了ApacheCommons文件上传和spring multipart,并将图像存储在目录文件夹中(不在项目上下文中)。 我遇到的问题是获取图像并在jsp上进行渲染。我尝试使用缓冲图像和ImageIO从该文件夹中读取图像,但不知道如何使用img标记在jsp中进行渲染。 任何帮助都将不胜感激Java 使用spring和apache common from文件夹在web上显示图像,java,spring-mvc,apache-commons-fileupload,Java,Spring Mvc,Apache Commons Fileupload,在我的应用程序中,我有一个上传功能。为此,我使用了ApacheCommons文件上传和spring multipart,并将图像存储在目录文件夹中(不在项目上下文中)。 我遇到的问题是获取图像并在jsp上进行渲染。我尝试使用缓冲图像和ImageIO从该文件夹中读取图像,但不知道如何使用img标记在jsp中进行渲染。 任何帮助都将不胜感激 //Code for reading BufferedImage im=ImageIO.read(new File(imagePath)); 最终能够使用i
//Code for reading
BufferedImage im=ImageIO.read(new File(imagePath));
最终能够使用img标签在web浏览器上显示图像。 我现在遵循的步骤是: 1.我使用BuffereImage读取图像。 2.使用ByteArrayOutputStream将BuffereImage转换为字节。 3.使用apache commons编解码器库将该流编码为Base64,并转换为字符串 4.使用img标记返回html上图像的此字符串值
//Pseudo Code
BufferedImage bufferedImage=ImageIO.read(new File(imagePath));
//imageDao contains the image name that i stored in the database
String []formatSplit=imageDao.split("\\.");
if(formatSplit.length==2){
String format=formatSplit[1];
//ImageUtility is class that contain code for converting bufferedimage to string
String traineeImage=ImageUtility.encodeToString(bufferedImage,format );
model.addAttribute("imagePath", traineeImage);
}
//ImageUtilty class -method
public static String encodeToString(BufferedImage image, String type) {
String imageString=null;
String encodedImage=null;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
ImageIO.write(image, type, bos);
byte[] imageBytes = bos.toByteArray();
encodedImage=org.apache.commons.codec.binary.Base64.encodeBase64String(imageBytes);
imageString = "data:image/"+type+";base64,"+encodedImage;
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
return imageString;
}
在img标记src属性中,我传递了imageString,它成功了。
为了找到解决方案,我从stackoverflow和其他博客中找到了很多提示,帮助我实现了我想要的目标。
谢谢。终于可以使用img标签在web浏览器上显示图像了。 我现在遵循的步骤是: 1.我使用BuffereImage读取图像。 2.使用ByteArrayOutputStream将BuffereImage转换为字节。 3.使用apache commons编解码器库将该流编码为Base64,并转换为字符串 4.使用img标记返回html上图像的此字符串值
//Pseudo Code
BufferedImage bufferedImage=ImageIO.read(new File(imagePath));
//imageDao contains the image name that i stored in the database
String []formatSplit=imageDao.split("\\.");
if(formatSplit.length==2){
String format=formatSplit[1];
//ImageUtility is class that contain code for converting bufferedimage to string
String traineeImage=ImageUtility.encodeToString(bufferedImage,format );
model.addAttribute("imagePath", traineeImage);
}
//ImageUtilty class -method
public static String encodeToString(BufferedImage image, String type) {
String imageString=null;
String encodedImage=null;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
ImageIO.write(image, type, bos);
byte[] imageBytes = bos.toByteArray();
encodedImage=org.apache.commons.codec.binary.Base64.encodeBase64String(imageBytes);
imageString = "data:image/"+type+";base64,"+encodedImage;
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
return imageString;
}
在img标记src属性中,我传递了imageString,它成功了。
为了找到解决方案,我从stackoverflow和其他博客中找到了很多提示,帮助我实现了我想要的目标。
谢谢。终于可以使用img标签在web浏览器上显示图像了。 我现在遵循的步骤是: 1.我使用BuffereImage读取图像。 2.使用ByteArrayOutputStream将BuffereImage转换为字节。 3.使用apache commons编解码器库将该流编码为Base64,并转换为字符串 4.使用img标记返回html上图像的此字符串值
//Pseudo Code
BufferedImage bufferedImage=ImageIO.read(new File(imagePath));
//imageDao contains the image name that i stored in the database
String []formatSplit=imageDao.split("\\.");
if(formatSplit.length==2){
String format=formatSplit[1];
//ImageUtility is class that contain code for converting bufferedimage to string
String traineeImage=ImageUtility.encodeToString(bufferedImage,format );
model.addAttribute("imagePath", traineeImage);
}
//ImageUtilty class -method
public static String encodeToString(BufferedImage image, String type) {
String imageString=null;
String encodedImage=null;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
ImageIO.write(image, type, bos);
byte[] imageBytes = bos.toByteArray();
encodedImage=org.apache.commons.codec.binary.Base64.encodeBase64String(imageBytes);
imageString = "data:image/"+type+";base64,"+encodedImage;
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
return imageString;
}
在img标记src属性中,我传递了imageString,它成功了。
为了找到解决方案,我从stackoverflow和其他博客中找到了很多提示,帮助我实现了我想要的目标。
谢谢。终于可以使用img标签在web浏览器上显示图像了。 我现在遵循的步骤是: 1.我使用BuffereImage读取图像。 2.使用ByteArrayOutputStream将BuffereImage转换为字节。 3.使用apache commons编解码器库将该流编码为Base64,并转换为字符串 4.使用img标记返回html上图像的此字符串值
//Pseudo Code
BufferedImage bufferedImage=ImageIO.read(new File(imagePath));
//imageDao contains the image name that i stored in the database
String []formatSplit=imageDao.split("\\.");
if(formatSplit.length==2){
String format=formatSplit[1];
//ImageUtility is class that contain code for converting bufferedimage to string
String traineeImage=ImageUtility.encodeToString(bufferedImage,format );
model.addAttribute("imagePath", traineeImage);
}
//ImageUtilty class -method
public static String encodeToString(BufferedImage image, String type) {
String imageString=null;
String encodedImage=null;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
ImageIO.write(image, type, bos);
byte[] imageBytes = bos.toByteArray();
encodedImage=org.apache.commons.codec.binary.Base64.encodeBase64String(imageBytes);
imageString = "data:image/"+type+";base64,"+encodedImage;
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
return imageString;
}
在img标记src属性中,我传递了imageString,它成功了。
为了找到解决方案,我从stackoverflow和其他博客中找到了很多提示,帮助我实现了我想要的目标。
谢谢