【问题标题】:Resizing a picture on a jsp page that's stored in a database调整存储在数据库中的 jsp 页面上的图片大小
【发布时间】:2013-04-20 16:18:45
【问题描述】:

我正在尝试在我从数据库中提取的 jsp 页面上设置图片的大小。

我可以得到图像,但它会填满整个页面。

谁知道怎么设置图片的大小

谢谢

这是我的 jsp 上的代码:

<%

Blob image = null;  
byte[] imgData = null;  
Connection con;
String url="jdbc:mysql://localhost:3306/comshopDatabase";
String uName="root";
String pwd="";
Class.forName("com.mysql.jdbc.Driver").newInstance();
    con=DriverManager.getConnection(url,uName,pwd);
    String sql="Select image from images WHERE id = '1' ";
    PreparedStatement stmt=con.prepareStatement(sql);

    ResultSet resultset=stmt.executeQuery();
while(resultset.next())
{

    Blob bl = resultset.getBlob("image");
    byte[] pict = bl.getBytes(1,(int)bl.length());
    response.setContentType("image/jpg");
    OutputStream o = response.getOutputStream();



%>
<TABLE BORDER="1">
<TR>
<TH>picture</TH>
</TR>
<TR>


<td>Image</td><td><%o.write(pict);%></td>
    <%o.flush();
    o.close();%>

<TD> <%= resultset.getString(2) %> </TD>
<TD><%= resultset.getString(3) %></TD>
</TR>
</TABLE>
<BR>
<%
o.flush();
o.close();
}
%>

【问题讨论】:

  • 这是一种糟糕的做法。使用servlet
  • 我有一个用于上传的servlet,这段代码只是为了测试显示

标签: java database jsp blob


【解决方案1】:

你可以使用image scaling library,jar可以从here下载。

下面是一个简单的例子:

int width =1200, height = 900;
BufferedImage image = ImageIO.read(<sourceFile>);// You can use InputStream as well here, in place of source file
BufferedImage thumbnail = Scalr.resize(image, Scalr.Method.SPEED, Scalr.Mode.FIT_TO_WIDTH,
                                       width, height , Scalr.OP_ANTIALIAS);
ImageIO.write(thumbnail, "jpg", new File(<destination file>)); // You can use OutputStream as well in place of destination file

【讨论】:

  • 我可以把这部分代码放在servlet BufferedImage image = ImageIO.read();然后在 jsp 中的其余部分?
  • 你应该把这段代码放在你想重新调整图像大小的任何地方。我建议您将此代码放在某个实用程序类中。从 servlet 传递 InputStream 到这个实用方法并取回调整大小的 OutputStream
猜你喜欢
  • 1970-01-01
  • 2021-11-26
  • 2012-10-02
  • 1970-01-01
  • 2017-10-09
  • 1970-01-01
  • 2021-06-05
  • 2017-05-25
  • 2023-04-04
相关资源
最近更新 更多