struts2 从数据库中读取图片流文件,并显示到jsp

本文介绍了如何在Struts框架中进行文件操作,包括图片、Excel文件的处理及响应设置。此外,还提供了文件流读取展示的方法,并展示了文本文件读取及邮件消息体设置的过程。最后,给出了文件及文本的加密与解密示例。

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

弹出保存路径:

js:

window.open(str,'blank_','scrollbars=no,resizable=no,width=10,height=10,menubar=no');

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

struts中:

//图片

response.setContentType("image/jpg");
response.setHeader("Content-disposition",
"attachment;filename=\"" + mail.getFileName() + "\";");
BufferedImage image = null;
image=ImageIO.read(in);
ServletOutputStream sos = response.getOutputStream();
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(sos);
encoder.encode(image);
in.close();



//excel

response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-disposition",
"attachment;filename=\"" + mail.getFileName() + "\";");
OutputStream out = response.getOutputStream();
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet s = wb.createSheet();
int len = 0;
byte[] buf = new byte[1];
while((len = in.read(buf)) != -1)
{
out.write(buf, 0, len);
}
wb.write(out);
out.close();
return null;



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

将图片流从数控库取出,显示到JSP

//jpg

HttpServletResponse response = (HttpServletResponse)
ActionContext.getContext().get(org.apache.struts2.StrutsStatics.HTTP_RESPONSE);

byte[] buf = new byte[1];
response.setContentType("image/jpeg");
OutputStream os=response.getOutputStream();
int len = 0;
while((len = in.read(buf)) != -1)
{
os.write(buf, 0, len);
}
os.close();
in.close();
return null;



---jsp

<s:property value="response.getOutputStream()"/>



//text file



BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder sb = new StringBuilder();
String line = null;

while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
mail.setMessageBody(sb.toString());



//jsp

<s:property value="mailVO.messageBody"/>





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



public class Test1
{

public static void main(String[] args)
{
// attachmentEncryption();
attachmentDecryption();
}

public static void attachmentEncryption()
{
InputStream in = null;
byte[] buf = new byte[100];
int len = 0;
BasicBinaryEncryptor encryptor = new BasicBinaryEncryptor();
try
{
in = new FileInputStream(new File("D:\\file.txt"));
encryptor.setPassword("test");
byte[] myEncryptedBinary = encryptor.encrypt(InputStreamToByte(in));
InputStream sbs = new ByteArrayInputStream(myEncryptedBinary);

FileOutputStream os = new FileOutputStream(new File("D:\\temp.txt"));
while((len = sbs.read(buf)) != -1)
{
os.write(buf, 0, len);
}
in.close();
sbs.close();
os.close();

}
catch (IOException e)
{
e.printStackTrace();
}
}

public static void attachmentDecryption()
{
InputStream in = null;
FileOutputStream os = null;
byte[] buf = new byte[100];
int len = 0;
BasicBinaryEncryptor encryptor = new BasicBinaryEncryptor();
try
{
in = new FileInputStream(new File("D:\\temp.txt"));
encryptor.setPassword("test");
byte[] binary = encryptor.decrypt(InputStreamToByte(in));
InputStream sbs = new ByteArrayInputStream(binary);
os = new FileOutputStream(new File("D:\\result.txt"));
while((len = sbs.read(buf)) != -1)
{
os.write(buf, 0, len);
}
in.close();
sbs.close();
os.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}

public static byte[] InputStreamToByte(InputStream iStrm) throws IOException
{
ByteArrayOutputStream bytestream = new ByteArrayOutputStream();
int ch;
while ((ch = iStrm.read()) != -1)
{
bytestream.write(ch);
}
byte imgdata[]=bytestream.toByteArray();
bytestream.close();
return imgdata;
}
public static void textEncryption()
{
BasicTextEncryptor textEncryptor = new BasicTextEncryptor();
textEncryptor.setPassword("test");
String myEncryptedText = textEncryptor.encrypt("this's a encrypted mail");
System.out.println("encrypted text: "+myEncryptedText);
String plainText = textEncryptor.decrypt(myEncryptedText);
System.out.println("plain text: "+plainText);
}
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值