前言:今天做个功能需要展示图片到页面,并不是下载,在网上搜了老半天,大部分都是下载,有的话也是只能在IE下进行输出,其它浏览器就都是下载了。
Action代码:
- public String processImage(){
- String key = ServletActionContext.getRequest().getParameter("key");
- if(StringUtils.isEmpty(key)){
- return "nofindImage";//没有找到图片
- }
- ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
- .processDefinitionKey(key)
- .singleResult();
- String diagramResourceName = processDefinition.getDiagramResourceName();
- InputStream imageStream = repositoryService.getResourceAsStream(processDefinition.getDeploymentId(), diagramResourceName);
- ServletActionContext.getContext().put("imageStream", imageStream);
- return "processImage";
- }
注:这里将InputStream放到了Struts2的对象站里面
此外,也可以通过在action中添加一个getInputName()的方法,在这个方法里面返回上面的InputStream
XML配置:在action中添加结果
- <result name="processImage" type="stream">
- <param name="contentType">image/png</param>
- <param name="inputName">imageStream</param>
- <param name="bufferSize">4096</param>
- </result>
注:inputName的值对应了我们在action中放入对象栈的key,因为StreamResult的源码中获得inputName就是从栈中获取。
其它说明:网上部分代码是在action中将InputStream通过Response直接输出,并返回一个null,这样在ie下是可以直接显示图片,在其他浏览器里面都是下载的方式。
注册需求中,图片验证码是很普遍要求的功能
啥也不多说了,上代码
- import java.awt.Color;
- import java.awt.Font;
- import java.awt.Graphics;
- import java.awt.Graphics2D;
- import java.awt.image.BufferedImage;
- import java.io.ByteArrayInputStream;
- import java.io.ByteArrayOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.util.Random;
- import javax.imageio.ImageIO;
- import javax.imageio.stream.ImageOutputStream;
- import org.apache.struts2.ServletActionContext;
- import raisi.core.action.BaseApi;
- public class ValidatePic extends BaseApi {
- private static final long serialVersionUID = -4159857994104900410L;
- @Override
- public Object getModel() {
- return null;
- }
- private static final int WIDTH = 120;
- private static final int HEIGHT = 35;
- public String getValidate() throws IOException{
- //准备一张图片
- BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_3BYTE_BGR);
- //得到图片对象
- Graphics g =image.getGraphics();
- //1.设置背景色
- setBackGround(g);
- //2.设置边框
- setBorder(g);
- //3.画多条干扰线
- drawRandowLine(g);
- //4.写随机文字或数字
- String random = drawText((Graphics2D)g);
- request.getSession().setAttribute("checkcode", random);
- ByteArrayOutputStream bs =new ByteArrayOutputStream();
- ImageOutputStream imOut =ImageIO.createImageOutputStream(bs);
- ImageIO.write(image,"jpg",imOut);
- InputStream is =new ByteArrayInputStream(bs.toByteArray());
- ServletActionContext.getContext().put("imageStream", is);
- return "image";
- }
- private String drawText(Graphics2D g) {
- g.setColor(Color.RED);
- g.setFont(new Font("宋体", Font.BOLD, 20)); //设置字体,宋体、粗体、大小
- StringBuffer sb = new StringBuffer(); //存放验证码
- //常用汉字码表
- String base="u7684u4e00u662fu4e86u6211u4e0du4ebau5728u4ed6u6709u8fd9u4e2au4e0au4eecu6765u5230u65f6u5927" +
- "u5730u4e3au5b50u4e2du4f60u8bf4u751fu56fdu5e74u7740u5c31u90a3u548cu8981u5979u51fau4e5fu5f97u91cc" +
- "u540eu81eau4ee5u4f1au5bb6u53efu4e0bu800cu8fc7u5929u53bbu80fdu5bf9u5c0fu591au7136u4e8eu5fc3u5b66" +
- "u4e48u4e4bu90fdu597du770bu8d77u53d1u5f53u6ca1u6210u53eau5982u4e8bu628au8fd8u7528u7b2cu6837u9053" +
- "u60f3u4f5cu79cdu5f00u7f8eu603bu4eceu65e0u60c5u5df1u9762u6700u5973u4f46u73b0u524du4e9bu6240u540c" +
- "u65e5u624bu53c8u884cu610fu52a8u65b9u671fu5b83u5934u7ecfu957fu513fu56deu4f4du5206u7231u8001u56e0" +
- "u5f88u7ed9u540du6cd5u95f4u65afu77e5u4e16u4ec0u4e24u6b21u4f7fu8eabu8005u88abu9ad8u5df2u4eb2u5176" +
- "u8fdbu6b64u8bddu5e38u4e0eu6d3bu6b63u611fu89c1u660eu95eeu529bu7406u5c14u70b9u6587u51e0u5b9au672c" +
- "u516cu7279u505au5916u5b69u76f8u897fu679cu8d70u5c06u6708u5341u5b9eu5411u58f0u8f66u5168u4fe1u91cd" +
- "u4e09u673au5de5u7269u6c14u6bcfu5e76u522bu771fu6253u592au65b0u6bd4u624du4fbfu592bu518du4e66u90e8" +
- "u6c34u50cfu773cu7b49u4f53u5374u52a0u7535u4e3bu754cu95e8u5229u6d77u53d7u542cu8868u5fb7u5c11u514b" +
- "u4ee3u5458u8bb8u7a1cu5148u53e3u7531u6b7bu5b89u5199u6027u9a6cu5149u767du6216u4f4fu96beu671bu6559" +
- "u547du82b1u7ed3u4e50u8272u66f4u62c9u4e1cu795eu8bb0u5904u8ba9u6bcdu7236u5e94u76f4u5b57u573au5e73" +
- "u62a5u53cbu5173u653eu81f3u5f20u8ba4u63a5u544au5165u7b11u5185u82f1u519bu5019u6c11u5c81u5f80u4f55" +
- "u5ea6u5c71u89c9u8defu5e26u4e07u7537u8fb9u98ceu89e3u53ebu4efbu91d1u5febu539fu5403u5988u53d8u901a" +
- "u5e08u7acbu8c61u6570u56dbu5931u6ee1u6218u8fdcu683cu58ebu97f3u8f7bu76eeu6761u5462u75c5u59cbu8fbe" +
- "u6df1u5b8cu4ecau63d0u6c42u6e05u738bu5316u7a7au4e1au601du5207u600eu975eu627eu7247u7f57u94b1u7d36" +
- "u5417u8bedu5143u559cu66feu79bbu98deu79d1u8a00u5e72u6d41u6b22u7ea6u5404u5373u6307u5408u53cdu9898" +
- "u5fc5u8be5u8bbau4ea4u7ec8u6797u8bf7u533bu665au5236u7403u51b3u7aa2u4f20u753bu4fddu8bfbu8fd0u53ca" +
- "u5219u623fu65e9u9662u91cfu82e6u706bu5e03u54c1u8fd1u5750u4ea7u7b54u661fu7cbeu89c6u4e94u8fdeu53f8" +
- "u5df4u5947u7ba1u7c7bu672au670bu4e14u5a5au53f0u591cu9752u5317u961fu4e45u4e4eu8d8au89c2u843du5c3d" +
- "u5f62u5f71u7ea2u7238u767eu4ee4u5468u5427u8bc6u6b65u5e0cu4e9au672fu7559u5e02u534au70edu9001u5174" +
- "u9020u8c08u5bb9u6781u968fu6f14u6536u9996u6839u8bb2u6574u5f0fu53d6u7167u529eu5f3au77f3u53e4u534e" +
- "u8ae3u62ffu8ba1u60a8u88c5u4f3cu8db3u53ccu59bbu5c3cu8f6cu8bc9u7c73u79f0u4e3du5ba2u5357u9886u8282" +
- "u8863u7ad9u9ed1u523bu7edfu65adu798fu57ceu6545u5386u60cau8138u9009u5305u7d27u4e89u53e6u5efau7ef4" +
- "u7eddu6811u7cfbu4f24u793au613fu6301u5343u53f2u8c01u51c6u8054u5987u7eaau57fau4e70u5fd7u9759u963f" +
- "u8bd7u72ecu590du75dbu6d88u793eu7b97u4e49u7adfu786eu9152u9700u5355u6cbbu5361u5e78u5170u5ff5u4e3e" +
- "u4ec5u949fu6015u5171u6bdbu53e5u606fu529fu5b98u5f85u7a76u8ddfu7a7fu5ba4u6613u6e38u7a0bu53f7u5c45" +
- "u8003u7a81u76aeu54eau8d39u5012u4ef7u56feu5177u521au8111u6c38u6b4cu54cdu5546u793cu7ec6u4e13u9ec4" +
- "u5757u811au5473u7075u6539u636eu822cu7834u5f15u98dfu4ecdu5b58u4f17u6ce8u7b14u751au67d0u6c89u8840" +
- "u5907u4e60u6821u9ed8u52a1u571fu5faeu5a18u987bu8bd5u6000u6599u8c03u5e7fu8716u82cfu663eu8d5bu67e5" +
- "u5bc6u8baeu5e95u5217u5bccu68a6u9519u5ea7u53c2u516bu9664u8dd1u4eaeu5047u5370u8bbeu7ebfu6e29u867d" +
- "u6389u4eacu521du517bu9999u505cu9645u81f4u9633u7eb8u674eu7eb3u9a8cu52a9u6fc0u591fu4e25u8bc1u5e1d" +
- "u996du5fd8u8da3u652fu6625u96c6u4e08u6728u7814u73edu666eu5bfcu987fu7761u5c55u8df3u83b7u827au516d" +
- "u6ce2u5bdfu7fa4u7687u6bb5u6025u5eadu521bu533au5965u5668u8c22u5f1fu5e97u5426u5bb3u8349u6392u80cc" +
- "u6b62u7ec4u5ddeu671du5c01u775bu677fu89d2u51b5u66f2u9986u80b2u5fd9u8d28u6cb3u7eedu54e5u547cu82e5" +
- "u63a8u5883u9047u96e8u6807u59d0u5145u56f4u6848u4f26u62a4u51b7u8b66u8d1du8457u96eau7d22u5267u554a" +
- "u8239u9669u70dfu4f9du6597u503cu5e2eu6c49u6162u4f5bu80afu95fbu5531u6c99u5c40u4f2fu65cfu4f4eu73a9" +
- "u8d44u5c4bu51fbu901fu987eu6ceau6d32u56e2u5723u65c1u5802u5175u4e03u9732u56edu725bu54edu65c5u8857" +
- "u52b3u578bu70c8u59d1u9648u83abu9c7cu5f02u62b1u5b9du6743u9c81u7b80u6001u7ea7u7968u602au5bfbu6740" +
- "u5f8bu80dcu4efdu6c7du53f3u6d0bu8303u5e8au821eu79d8u5348u767bu697cu8d35u5438u8d23u4f8bu8ffdu8f83" +
- "u804cu5c5eu6e10u5de6u5f55u4e1du7259u515au7ee7u6258u8d76u7ae0u667au51b2u53f6u80e1u5409u5356u575a" +
- "u559du8089u9057u6551u4feeu677eu4e34u85cfu62c5u620fu5584u536bu836fu60b2u6562u9760u4f0au6751u6234" +
- "u8bcdu68eeu8033u5deeu77edu7956u4e91u89c4u7a97u6563u8ff7u6cb9u65e7u9002u4e61u67b6u6069u6295u5f39" +
- "u94c1u535au96f7u5e9cu538bu8d85u8d1fu52d2u6742u9192u6d17u91c7u6bebu5634u6bd5u4e5du51b0u65e2u72b6" +
- "u4e71u666fu5e2du73cdu7ae5u9876u6d3eu7d20u8131u519cu7591u7ec3u91ceu6309u72afu62cdu5f81u574fu9aa8" +
- "u4f59u627fu7f6eu81d3u5f69u706fu5de8u7434u514du73afu59c6u6697u6362u6280u7ffbu675fu589eu5fcdu9910" +
- "u6d1bu585eu7f3au5fc6u5224u6b27u5c42u4ed8u9635u739bu6279u5c9bu9879u72d7u4f11u61c2u6b66u9769u826f" +
- "u6076u604bu59d4u62e5u5a1cu5999u63a2u5440u8425u9000u6447u5f04u684cu719fu8bfau5ba3u94f6u52bfu5956" +
- "u5babu5ffdu5957u5eb7u4f9bu4f18u8bfeu9e1fu558au964du590fu56f0u5218u7f6au4ea1u978bu5065u6a21u8d25" +
- "u4f34u5b88u6325u9c9cu8d22u5b64u67aau7981u6050u4f19u6770u8ff9u59b9u85f8u904du76d6u526fu5766u724c" +
- "u6c5fu987au79cbu8428u83dcu5212u6388u5f52u6d6au542cu51e1u9884u5976u96c4u5347u7883u7f16u5178u888b" +
- "u83b1u542bu76dbu6d4eu8499u68cbu7aefu817fu62dbu91cau4ecbu70e7u8bef";
- int hor = 5;
- for(int i=0;i<4;i++){
- //获取旋转的度数
- int degree = (new Random().nextInt(50))-25;
- String text = base.charAt(new Random().nextInt(base.length()))+""; //得到随机文字
- sb.append(text); //将随机文字加入验证码
- g.rotate(degree*Math.PI/180, hor, 5); //设置旋转
- g.drawString(text, hor, 20);
- g.rotate(-degree*Math.PI/180, hor, 5); //为了不影响下一个随机文字的输出,旋转回去
- hor+=30;
- }
- System.out.println("sb--->"+sb);
- return sb.toString();
- }
- private void drawRandowLine(Graphics g) {
- g.setColor(Color.GREEN);
- for(int i=0;i<5;i++){
- int x1 = new Random().nextInt(WIDTH); //随机生成0~120之间的整数
- int x2 = new Random().nextInt(WIDTH); //随机生成0~120之间的整数
- int y1 = new Random().nextInt(HEIGHT); //随机生成0~25之间的整数
- int y2 = new Random().nextInt(HEIGHT); //随机生成0~25之间的整数
- g.drawLine(x1, y1, x2, y2);
- }
- }
- private void setBackGround(Graphics g) {
- g.setColor(Color.WHITE);
- g.fillRect(0, 0, WIDTH, HEIGHT); //白色铺满
- }
- private void setBorder(Graphics g) {
- g.setColor(Color.BLUE);
- g.drawRect(1, 1, WIDTH-2, HEIGHT-2);
- }
- }
struts配置如下
- <result name="image" type="stream">
- <param name="contentType">image/png</param>
- <param name="inputName">imageStream</param>
- <param name="bufferSize">4096</param>
- </result>
页面的调用如下
- <img src="<%=context %>/***!getValidate.action">
效果如下