JWorld@TW the best professional Java site in Taiwan
      註冊 | 登入 | 全文檢索 | 排行榜  

» JWorld@TW » Servlet/JSP 討論區  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友   
reply to topicthreaded modego to previous topicgo to next topic
本主題所含的標籤
無標籤
作者 沒什麼營養的動態生成圖片 Servlet & JSP [精華]
JiaYun



版主

發文: 761
積分: 9
於 2003-07-03 18:45 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
只是好奇 Jute 登入的安全校驗碼圖片可以怎麼產生, 所以亂弄了一下 Tongue

在 RedHat & Tomcat 下 Servlet, JSP 都能正確產生圖檔

Resin 卻可能遇到

java.lang.InternalError: Can't connect to X11 window server using ':0.0' as the value of the DISPLAY variable.

如果 JDK 是 1.4 以後版本

可以在啟動 resin 的指令後面加上 -verbose -J-Djava.awt.headless=true 例:

./httpd.sh -restart -verbose -J-Djava.awt.headless=true

或者修改 wrapper.pl

#
# Additional args to pass to java.
#
$JAVA_ARGS="-Djava.awt.headless=true";

不過我試的結果, 雖然 Servlet 可以正常產生圖片

但 JSP 產生的檔案開頭卻是多了幾行空行, 造成瀏覽器或圖檔檢視器無法辨別格式

附件裡的 DynaImageResinJSP.jpg 就是有問題的圖

DynaImage.zip (4.03k)


JiaYun edited on 2003-07-03 19:31
reply to postreply to post
作者 Re:沒什麼營養的動態生成圖片 Servlet & JSP [Re:JiaYun]
iampoya

Speculator

版主

發文: 169
積分: 8
於 2003-07-03 18:55 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
真是佩服你的好奇心跟研究精神

--
忍不住…又灌水了 :P


reply to postreply to post
作者 Re:沒什麼營養的動態生成圖片 Servlet & JSP [Re:JiaYun]
koji

秒速5センチメートル

站長

發文: 8415
積分: 19
於 2003-07-03 19:13 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
怎麼樣叫無法辨別格式阿???
因為我執行以後還是可以看到檔案阿呵呵

另外你說會產生一點空白
會不會是轉成servlet時CONTAINER給你加的
在TOMCAT下他會多了
out.write("\n");
out.write("\n");
out.write("\n");
out.write("\n");
out.write("\n");
out.write("\n");
out.write("\n");
out.write("\n");
所以在一開始先out.clear()如何勒

good luck

koji


reply to postreply to post
作者 Re:沒什麼營養的動態生成圖片 Servlet & JSP [Re:koji]
JiaYun



版主

發文: 761
積分: 9
於 2003-07-03 19:18 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
koji wrote:
所以在一開始先out.clear()如何勒
good luck
koji


果然好了, koji 真厲害 Big Smile

不過不要一直 good luck 啦 Cry


reply to postreply to post
作者 Re:沒什麼營養的動態生成圖片 Servlet & JSP [Re:koji]
JiaYun



版主

發文: 761
積分: 9
於 2003-07-03 19:27 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
koji wrote:
怎麼樣叫無法辨別格式阿???
因為我執行以後還是可以看到檔案阿呵呵


koji 想知道怎麼樣叫無法辨別格式 Tongue

Mozilla 會這樣:

無法顯示圖片「網址」,原因:檔案內容有錯。

一些 Linux 下的看圖軟體也沒辦法顯示


reply to postreply to post
作者 Re:沒什麼營養的動態生成圖片 Servlet & JSP [Re:JiaYun]
koji

秒速5センチメートル

站長

發文: 8415
積分: 19
於 2003-07-03 19:36 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
hmm
感恩
因為我在WIN下IE和ACDSEE都看的到
所以一時不知道為啥

謝囉^_^

--
(人ω-)謝謝(-ω人)謝謝


reply to postreply to post
作者 Re:沒什麼營養的動態生成圖片 Servlet & JSP [Re:JiaYun]
sdargon





發文: 109
積分: 0
於 2003-07-21 17:13 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
我處理jsp產生的空白都是....
讓%>的後面不接換行,而是直接接<%
原來...還有out.clear()可以用呀...
我...居然笨了那麼久Black Eye


reply to postreply to post
作者 Re:沒什麼營養的動態生成圖片 Servlet & JSP [Re:JiaYun]
jeff0208





發文: 11
積分: 0
於 2003-07-22 14:31 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
我也沒有用過耶
果然經常上來逛逛
還是每次都可以學到一些東西

Big Smile


reply to postreply to post
作者 Re:沒什麼營養的動態生成圖片 Servlet & JSP [Re:JiaYun]
garbage

以死美工自居



發文: 65
積分: 0
於 2003-07-26 06:15 user profilesend a private message to usersend email to garbagereply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
是阿是阿...
有些東西不講還真的是不知道哩...


reply to postreply to post
作者 Re:沒什麼營養的動態生成圖片 Servlet & JSP [Re:JiaYun]
freeman_z





發文: 10
積分: 0
於 2004-07-05 12:30 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
<%@ page import="javax.imageio.plugins.jpeg.*" %>

后面都不要换行,应该也可以的吧? -_-!


reply to postreply to post
作者 Re:沒什麼營養的動態生成圖片 Servlet & JSP [Re:JiaYun]
qboy113011





發文: 56
積分: 0
於 2005-05-11 08:46 user profilesend a private message to usersend email to qboy113011reply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
我在jsp頁面裡,加上這段程式碼,為什麼只會看到生成的圖片,而其他的東西都不會出現,謝謝

reply to postreply to post
作者 Re:沒什麼營養的動態生成圖片 Servlet & JSP [Re:JiaYun]
qboy113011





發文: 56
積分: 0
於 2005-05-11 21:04 user profilesend a private message to usersend email to qboy113011reply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
知道怎麼用了

<img src="DynaImage.jsp">即可

想在請教,它產生一個叫imageio10648.tmp的東西,該怎麼讓它不產生,或是有辦法指定它產生的路徑


reply to postreply to post
作者 Re:沒什麼營養的動態生成圖片 Servlet & JSP [Re:JiaYun]
uxa





發文: 489
積分: 6
於 2006-11-30 20:22 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
請教一下小弟我使用了這個圖片驗證後,在load到這一個頁面時它會丟出以下的exception
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
2006-11-30 20:19:37,463 ERROR [StandardWrapperValve.invoke:253]- Servlet.service() for servlet jsp threw exception
java.lang.IllegalStateException: getOutputStream() has already been called for this response
  at org.apache.catalina.connector.Response.getWriter(Response.java:599)
  at org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:195)
  at org.apache.jasper.runtime.JspWriterImpl.initOut(JspWriterImpl.java:124)
  at org.apache.jasper.runtime.JspWriterImpl.flushBuffer(JspWriterImpl.java:117)
  at org.apache.jasper.runtime.PageContextImpl.release(PageContextImpl.java:191)
  at org.apache.jasper.runtime.JspFactoryImpl.internalReleasePageContext(JspFactoryImpl.java:115)
  at org.apache.jasper.runtime.JspFactoryImpl.releasePageContext(JspFactoryImpl.java:75)
  at org.apache.jsp.jsp.portal.DynaImage_jsp._jspService(DynaImage_jsp.java:98)
  at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
  at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
  at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:332)
  at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
  at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
  at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
  at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
  at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
  at org.displaytag.filter.ResponseOverrideFilter.doFilter(ResponseOverrideFilter.java:125)
  at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
  at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
  at com.era.ticket.filter.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilter.java:122)
  at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
  at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
  at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
  at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
  at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
  at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
  at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
  at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
  at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:869)
  at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:664)
  at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
  at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
  at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
  at java.lang.Thread.run(Thread.java:595)
2006-11-30 20:19:37,463 ERROR [StandardWrapperValve.invoke:253]- Servlet.service() for servlet jsp threw exception
java.lang.IllegalStateException: getOutputStream() has already been called for this response
  at org.apache.catalina.connector.Response.getWriter(Response.java:599)
  at org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:195)
  at org.apache.jasper.runtime.JspWriterImpl.initOut(JspWriterImpl.java:124)
  at org.apache.jasper.runtime.JspWriterImpl.flushBuffer(JspWriterImpl.java:117)
  at org.apache.jasper.runtime.PageContextImpl.release(PageContextImpl.java:191)
  at org.apache.jasper.runtime.JspFactoryImpl.internalReleasePageContext(JspFactoryImpl.java:115)
  at org.apache.jasper.runtime.JspFactoryImpl.releasePageContext(JspFactoryImpl.java:75)
  at org.apache.jsp.jsp.portal.DynaImage_jsp._jspService(DynaImage_jsp.java:98)
  at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
  at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
  at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:332)
  at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
  at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
  at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
  at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
  at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
  at org.displaytag.filter.ResponseOverrideFilter.doFilter(ResponseOverrideFilter.java:125)
  at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
  at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
  at com.era.ticket.filter.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilter.java:122)
  at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
  at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
  at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
  at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
  at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
  at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
  at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
  at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
  at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:869)
  at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:664)
  at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
  at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
  at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
  at java.lang.Thread.run(Thread.java:595)
 


請問一下這是正常的現象嗎??
要如何能避免掉這個Exception的產生呢?
謝謝


reply to postreply to post
☆只要努力就有希望☆
作者 Re:沒什麼營養的動態生成圖片 Servlet & JSP [Re:JiaYun]
shasun





發文: 118
積分: 0
於 2006-12-04 18:24 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
來些雜訊, 避免有心人士使用OCR辨識, 呵呵...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Iterator;
 
import javax.imageio.ImageIO;
import javax.imageio.ImageWriter;
import javax.imageio.stream.ImageOutputStream;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
public class DynaImageServlet extends HttpServlet {
 
   public void doGet(HttpServletRequest request, HttpServletResponse response)
         throws ServletException,
         IOException {
 
      try {
         BufferedImage bufferedImage = new BufferedImage(50, 30, BufferedImage.TYPE_INT_RGB);
         Graphics2D bufferedGraphics = bufferedImage.createGraphics();
         bufferedGraphics.setColor(Color.WHITE);
         bufferedGraphics.fillRect(0, 0, 50, 30);
         bufferedGraphics.setColor(Color.BLACK);
         bufferedGraphics.drawRect(5, 5, 40, 20);
 
         String number = Integer.toString((int) (Math.random() * 10000));
         while (number.length() < 4) {
            number = "0" + number;
         }
 
         bufferedGraphics.setFont(new Font("Default", Font.BOLD, 13));
         bufferedGraphics.drawString(number, 10, 20);
 
         for (int i = 0; i < 20; i++) {
            int x = (int) (Math.random() * 40) + 5;
            int y = (int) (Math.random() * 20) + 5;
 
            bufferedGraphics.fillOval(x, y, 1, 1);
         }
 
         response.setContentType("image/jpeg");
         ServletOutputStream outstream = response.getOutputStream();
 
         Iterator ite = ImageIO.getImageWritersByFormatName("jpeg");
         ImageWriter imageWriter = (ImageWriter) ite.next();
 
         ImageOutputStream ios = ImageIO.createImageOutputStream(outstream);
         imageWriter.setOutput(ios);
         imageWriter.write(bufferedImage);
         ios.flush();
         outstream.close();
 
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
}
 


reply to postreply to post
作者 Re:沒什麼營養的動態生成圖片 Servlet & JSP [Re:JiaYun]
jen3





發文: 5
積分: 0
於 2006-12-07 17:26 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
獲益良多啊!!果然解決了我的困擾

reply to postreply to post
作者 Re:沒什麼營養的動態生成圖片 Servlet & JSP [Re:uxa]
ok12360000





發文: 192
積分: 0
於 2007-07-27 23:50 user profilesend a private message to usersend email to ok12360000reply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
小弟用了之後,解決了我的困擾。
可有二個問題想請問大大們。

一、背景顏色如何改變,我用 bufferedGraphics.setBackground(Color.gray);
怎麼都不會改變背景顏色呢?

二、可以將圖的背景去背(變成逶明的),也就是說,當我的網頁底色是淺綠色,驗證圖片的背景圖也會跟著是淺綠色。

謝謝大大們的回答。


reply to postreply to post
» JWorld@TW »  Servlet/JSP 討論區

reply to topicthreaded modego to previous topicgo to next topic
  已讀文章
  新的文章
  被刪除的文章
Jump to the top of page

JWorld@TW 本站商標資訊

Powered by Powerful JuteForum® Version Jute 1.5.8