struts2 从数据库中读取图片源文件,并显示到jsp
struts2 从数据库中读取图片流文件,并显示到jsp
弹出保存路径:
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);
}
}
弹出保存路径:
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);
}
}