我可以保存图像吗
我编写了一个 JSP 代码,可以在浏览器上显示图像。
但我也想将这张图片保存在服务器端。
我已经搜索了答案,但我仍然不知道如何保存。
我应该编写什么代码才能保存输出图像?
<%@ page import="java.awt.*" %>
<%@ page import="java.awt.image.*" %>
<%@ page import="java.sql.*" %>
<%@ page import="com.sun.image.codec.jpeg.*" %>
<%
Class.forName("org.gjt.mm.mysql.Driver");
java.sql.Connection connection = java.sql.DriverManager.getConnection("jdbc:mysql://localhost/clothes","root","clothes");
Statement statement0 = connection.createStatement();
ResultSet rs = statement0.executeQuery("select * from clothes");
int i= 20;
BufferedImage Image = new BufferedImage(600,380, BufferedImage.TYPE_INT_RGB);
Graphics2D graphics = (Graphics2D) Image.getGraphics();
while(rs.next()) {
String Product = rs.getString("clothes_name");
graphics.setColor(Color.white);
graphics.drawString( Product ,15,i);
int Stock = Integer.parseInt(rs.getString("Sales Record"));
if ( Stock >= 25 ) graphics.setColor(Color.green);
if ( Stock >= 15 && Stock < 25 ) graphics.setColor(Color.yellow);
if ( Stock < 15 ) graphics.setColor(Color.red);
graphics.fillRect( 20,i+5, (Stock*10), 10);
i=i+30;
}
rs.close();
connection.close();
response.reset();
ServletOutputStream OutStream = response.getOutputStream();
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(OutStream);
encoder.encode(Image);
//I want to save this image, how to do so?
OutStream.close();
%>
I have coded a JSP that will show the image on the browser.
But I also want to save this image in the server-side.
I have searched the answer but I still don't know how to save.
What should I code in order to save output image?
<%@ page import="java.awt.*" %>
<%@ page import="java.awt.image.*" %>
<%@ page import="java.sql.*" %>
<%@ page import="com.sun.image.codec.jpeg.*" %>
<%
Class.forName("org.gjt.mm.mysql.Driver");
java.sql.Connection connection = java.sql.DriverManager.getConnection("jdbc:mysql://localhost/clothes","root","clothes");
Statement statement0 = connection.createStatement();
ResultSet rs = statement0.executeQuery("select * from clothes");
int i= 20;
BufferedImage Image = new BufferedImage(600,380, BufferedImage.TYPE_INT_RGB);
Graphics2D graphics = (Graphics2D) Image.getGraphics();
while(rs.next()) {
String Product = rs.getString("clothes_name");
graphics.setColor(Color.white);
graphics.drawString( Product ,15,i);
int Stock = Integer.parseInt(rs.getString("Sales Record"));
if ( Stock >= 25 ) graphics.setColor(Color.green);
if ( Stock >= 15 && Stock < 25 ) graphics.setColor(Color.yellow);
if ( Stock < 15 ) graphics.setColor(Color.red);
graphics.fillRect( 20,i+5, (Stock*10), 10);
i=i+30;
}
rs.close();
connection.close();
response.reset();
ServletOutputStream OutStream = response.getOutputStream();
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(OutStream);
encoder.encode(Image);
//I want to save this image, how to do so?
OutStream.close();
%>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用大部分相同的代码。您只需替换
为
与具体问题无关,请注意 Java命名约定规定变量名应该以小写字母开头。另请注意,在 JSP 文件而不是 Java 类中执行此操作是不最佳实践。它使得 Java 代码无法被普通 Java 类重用。
You can use much of the same code. You just need to replace
by
Unrelated to the concrete problem, please note that Java naming conventions state that variable names ought to start with lowercase. Also note that doing this in a JSP file instead of a Java class is not the best practice. It makes the Java code unreuseable by normal Java classes.