我可以保存图像吗

发布于 2024-10-17 05:31:17 字数 1603 浏览 1 评论 0原文

我编写了一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情话难免假 2024-10-24 05:31:17

您可以使用大部分相同的代码。您只需替换

ServletOutputStream OutStream = response.getOutputStream();

OutputStream OutStream = new FileOutputStream("/path/to/file.jpg");

与具体问题无关,请注意 Java命名约定规定变量名应该以小写字母开头。另请注意,在 JSP 文件而不是 Java 类中执行此操作是不最佳实践。它使得 Java 代码无法被普通 Java 类重用。

You can use much of the same code. You just need to replace

ServletOutputStream OutStream = response.getOutputStream();

by

OutputStream OutStream = new FileOutputStream("/path/to/file.jpg");

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文