从数据库读出的JPG文件的字符流,转换成图片显示在页面上的相关代码

本文介绍如何使用Java从数据库中读取图片,并将其直接显示在网页上或保存为文件。通过设置HTTP响应类型为图像格式,可以将查询结果中的二进制流转换为图像输出。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

public void doPost(HttpServletRequest req, HttpServletResponse res)
    throws ServletException, IOException {
    ServletOutputStream out = res.getOutputStream();
    Statement dispStmt = null, setStmt = null;
    try {
        dispStmt = Con.createStatement();
        setStmt = Con.createStatement();
        setStmt.executeUpdate("set textsize 2048000");
    } catch (Exception e) {
        out.println("Create Statement error:" + e.toString() + "<br>");
    }

    String ls_sql = null;
    ls_sql = req.getParameter("ImageSQL");
    if (ls_sql == null)
        ls_sql = "";
    ls_sql = ls_sql.trim();
    if (!ls_sql.equals("")) {
        ResultSet rs = null;
        try {
            rs = dispStmt.executeQuery(ls_sql);
        } catch (Exception e) {
            System.out.println(
                "Unable to Exec Statment" + e.toString() + "<br>");
        }
        try {
            while (rs.next()) {
                try {
                    res.setContentType("image/jpeg");
                    InputStream is = rs.getBinaryStream(1);
                    int size = is.available();
                    byte[] bzp = new byte[size];
                    is.read(bzp);
                    out.write(bzp);
                } catch (Exception e) {
                    System.out.println("Wirte image error: " + e.toString());
                }
            }
            rs.close();
        } catch (Exception e) {
            System.out.println("Unable to Close Statment" + e.toString());
        }
    }
    try {
        dispStmt.close();
    } catch (Exception e) {
        System.out.println("Close Statement Error: " + e.toString());
    }
    out.close();
}

--------------------------------------------------------------------------------

再附一个:

从库中读图片显示到页面上的主要源码
 <%@ page contentType="text/html;charset=iso8859_1"%>
<%@ page import="java.sql.ResultSet"%>
<jsp:useBean id="query" class="pub.MakeDB"/>
<%
String sql = "select * from images";
ResultSet rs = query.executeQuery(sql);
rs.next();
InputStream in = rs.getBinaryStream("image");
ServletOutputStream sos = response.getOutputStream();
int len = 0;
byte[] b = new byte[1024];
response.reset();
response.setContentType("image/gif");
while((len = in.read(b)) > 0)
sos.write(b,0,len);
in.close();
sos.close();
query.cls();
%>
如果是写到文件只要把sos
定义为DataOutputStream sos = new DataOutputStream(new BufferedOutputStream(new FileOutputStream("/a.gif")));

 
以下是一个用C语言实现从键盘输入字符串,将其中的大写字母转换成小写字母后存入磁盘文件,并从文件读出显示在屏幕上的程序: ```c #include <stdio.h> #include <stdlib.h> #include <ctype.h> #define MAX_LENGTH 1000 int main() { char input[MAX_LENGTH]; char output[MAX_LENGTH]; FILE *file; // 从键盘输入字符串 printf("请输入一个字符串:"); fgets(input, MAX_LENGTH, stdin); // 将大写字母转换成小写字母 for (int i = 0; input[i] != '\0'; i++) { output[i] = tolower(input[i]); } output[strcspn(input, "\n")] = '\0'; // 去除换行符 // 将结果写入磁盘文件 file = fopen("output.txt", "w"); if (file == NULL) { printf("无法打开文件进行写入。\n"); return 1; } fprintf(file, "%s", output); fclose(file); // 从磁盘文件读出内容并显示在屏幕上 file = fopen("output.txt", "r"); if (file == NULL) { printf("无法打开文件进行读取。\n"); return 1; } printf("转换后的字符串:\n"); while (fgets(output, MAX_LENGTH, file) != NULL) { printf("%s\n", output); } fclose(file); return 0; } ``` 这个程序的工作流程如下: 1. 使用`fgets`函数从键盘读取输入的字符串。 2. 遍历输入字符串,将每个大写字母转换为小写字母。使用`tolower`函数实现转换。 3. 将转换后的字符串写入名为"output.txt"的文件中。 4. 从"output.txt"文件中读取内容,并显示在屏幕上。 注意:这个程序假设输入的字符串长度不会超过`MAX_LENGTH-1`个字符。如果需要处理更长的字符串,可以增加`MAX_LENGTH`的值。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值