资源描述
do_upload.jsp
Java代码
1. <%@ page language="java" contentType="text/html; charset=GB18030"
2. pageEncoding="GB2312"%>
3. <%@ page import="mons.fileupload.*"%>
4. <%@ page import="mons.fileupload.servlet.*"%>
5. <%@ page import="mons.fileupload.disk.*"%>
6. <%@ page import="java.util.*"%>
7. <%@ page import="java.io.*"%>
8. <%@ page import="picDeal.*" %>
9. <jsp:useBean id="pic" scope="page" class="picDeal.PicRgb">
10. </jsp:useBean>
11. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
12.
13. <html>
14. <head>
15. <meta http-equiv="Content-Type" content="text/html; charset=GB18030">
16. <title>文件上传</title>
17. </head>
18. <body>
19. <center>上传中.....</center>
20. <%
21. String uploadPath=application.getRealPath("/upload");
22. String picName=null;
23. int width=0,height=0;
24. //目标文件夹的路径必须使用绝对路径,不能用相对路径
25. boolean isMultipart = ServletFileUpload.isMultipartContent(request);
26. if(isMultipart==true){
27. try{
28. FileItemFactory factory = new DiskFileItemFactory();
29. ServletFileUpload upload = new ServletFileUpload(factory);
30. List<FileItem> items = upload.parseRequest(request);//得到所有的文件
31. Iterator<FileItem> itr = items.iterator();
32. while(itr.hasNext()){//依次处理每个文件
33. FileItem item=(FileItem)itr.next();
34. String fileName=item.getName();//获得文件名,包括路径
35. if(fileName!=null)
36. {
37. File fullFile=new File(item.getName());
38. picName=fullFile.getName();
39. File savedFile=new File(uploadPath,fullFile.getName());
40. item.write(savedFile);
41. }
42. }
43. out.println("upload succeed!");
44. out.println("上传的图片如下:");
45. //分析图片, 获取图片的信息
46. pic.setData(picName);
47. session.setAttribute("picture",pic);
48. %>
49. <form action="anaresult.jsp" method="post">
50. <table>
51. <tr>
52. <td>上传的图片如下:</td>
53. </tr>
54. <tr>
55. <td><img src=<%= "upload/"+picName %>></td>
56. </tr>
57. <tr>
58. <td><input type="submit" name="submit" value="提交分析"></td>
59. </tr>
60. </table>
61. </form>
62. <%
63. }
64. catch(Exception e){
65. e.printStackTrace();
66. out.print("failed!");
67. }
68. }
69. else{
70. out.println("the enctype must be multipart/form-data");
71. }
72. %>
73. </body>
74. </html>
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB2312"%>
<%@ page import="mons.fileupload.*"%>
<%@ page import="mons.fileupload.servlet.*"%>
<%@ page import="mons.fileupload.disk.*"%>
<%@ page import="java.util.*"%>
<%@ page import="java.io.*"%>
<%@ page import="picDeal.*" %>
<jsp:useBean id="pic" scope="page" class="picDeal.PicRgb">
</jsp:useBean>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>文件上传</title>
</head>
<body>
<center>上传中.....</center>
<%
String uploadPath=application.getRealPath("/upload");
String picName=null;
int width=0,height=0;
//目标文件夹的路径必须使用绝对路径,不能用相对路径
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
if(isMultipart==true){
try{
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
List<FileItem> items = upload.parseRequest(request);//得到所有的文件
Iterator<FileItem> itr = items.iterator();
while(itr.hasNext()){//依次处理每个文件
FileItem item=(FileItem)itr.next();
String fileName=item.getName();//获得文件名,包括路径
if(fileName!=null)
{
File fullFile=new File(item.getName());
picName=fullFile.getName();
File savedFile=new File(uploadPath,fullFile.getName());
item.write(savedFile);
}
}
out.println("upload succeed!");
out.println("上传的图片如下:");
//分析图片, 获取图片的信息
pic.setData(picName);
session.setAttribute("picture",pic);
%>
<form action="anaresult.jsp" method="post">
<table>
<tr>
<td>上传的图片如下:</td>
</tr>
<tr>
<td><img src=<%= "upload/"+picName %>></td>
</tr>
<tr>
<td><input type="submit" name="submit" value="提交分析"></td>
</tr>
</table>
</form>
<%
}
catch(Exception e){
e.printStackTrace();
out.print("failed!");
}
}
else{
out.println("the enctype must be multipart/form-data");
}
%>
</body>
</html>
PicRgb
Java代码
1. package picDeal;
2. import java.awt.*;
3. import java.awt.image.*;
4. import java.io.*;
5.
6. import javax.imageio.*;
7.
8. public class PicRgb {
9. private int []rData;
10. private int []gData;
11. private int []bData;
12. private int width;
13. private int height;
14. //分析图片,参数为图片的名字
15. public PicRgb()
16. {
17.
18. }
19. public void setData(String name){
20. Image image=null;
21. try{
22. File sourceimage = new File("upload/"+name);
23. image=ImageIO.read(sourceimage);
24. }
25. catch(IOException e){
26. }
27. //读取图像的高度&宽度
28. this.width=image.getWidth(null);
29. this.height=image.getHeight(null);
30. System.out.println(width);
31. System.out.println(height);
32. //读取图片像素信息
33. Image im=Toolkit.getDefaultToolkit().getImage("upload/"+name);
34. this.rData=new int[256];
35. this.gData=new int[256];
36. this.bData=new int[256];
37. int []pixels=new int[width*height];
38. try{
39. PixelGrabber pg=new PixelGrabber(im,0,0,width,height,pixels,0,width);
40. pg.grabPixels();
41. }catch(InterruptedException e){
42. e.printStackTrace();
43. }
44. ColorModel cm=ColorModel.getRGBdefault();
45. int red,green,blue;
46. for(int i=0;i<width*height;i++){
47. red=cm.getRed(pixels[i]);
48. this.rData[red]++;
49. green=cm.getGreen(pixels[i]);
50. this.gData[green]++;
51. blue=cm.getBlue(pixels[i]);
52. this.bData[blue]++;
53. }
54. }
55. public void setRData(int[] r)
56. {
57. this.rData=r;
58. }
59. public void setGData(int[] g)
60. {
61. this.gData=g;
62. }
63. public void setBData(int[] b)
64. {
65. this.bData=b;
66. }
67. public void setWidth(int w)
68. {
69. this.width=w;
70. }
71. public void setHeight(int h)
72. {
73. this.height=h;
74. }
75. public int getWidth()
76. {
77. return this.width;
78. }
79. public int getHeight()
80. {
81. return this.height;
82. }
83. public int[] getRData()
84. {
85. return this.rData;
86. }
87. public int[] getGData()
88. {
89. return this.gData;
90. }
91. public int[] getBData()
92. {
93. return this.bData;
94. }
95. }
package picDeal;
import java.awt.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
public class PicRgb {
private int []rData;
private int []gData;
private int []bData;
private int width;
private int height;
//分析图片,参数为图片的名字
public PicRgb()
{
}
public void setData(String name){
Image image=null;
try{
File sourceimage = new File("upload/"+name);
image=ImageIO.read(sourceimage);
}
catch(IOException e){
}
//读取图像的高度&宽度
this.width=image.getWidth(null);
this.height=image.getHeight(null);
System.out.println(width);
System.out.println(height);
//读取图片像素信息
Image im=Toolkit.getDefaultToolkit().getImage("upload/"+name);
this.rData=new int[256];
this.gData=new int[256];
this.bData=new int[256];
int []pixels=new int[width*height];
try{
PixelGrabber pg=new PixelGrabber(im,0,0,width,height,pixels,0,width);
pg.grabPixels();
}catch(InterruptedException e){
e.printStackTrace();
}
ColorModel cm=ColorModel.getRGBdefault();
int red,green,blue;
for(int i=0;i<width*height;i++){
red=cm.getRed(pixels[i]);
this.rData[red]++;
green=cm.getGreen(pixels[i]);
this.gData[green]++;
blue=cm.getBlue(pixels[i]);
this.bData[blue]++;
}
}
public void setRData(int[] r)
{
this.rData=r;
}
public void setGData(int[] g)
{
this.gData=g;
}
public void setBData(int[] b)
{
this.bData=b;
}
public void setWidth(int w)
{
this.width=w;
}
public void setHeight(int h)
{
this.height=h;
}
public int getWidth()
{
return this.width;
}
public int getHeight()
{
return this.height;
}
public int[] getRData()
{
return this.rData;
}
public int[] getGData()
{
return this.gData;
}
public int[] getBData()
{
return this.bData;
}
}
当使用up_load.jsp页面时 出现错误
提示 useBean使用的类不可用……弄了一下午,头都大了 - -
采纳的答案
2009-05-18 studying_ (资深程序员)
<jsp:useBean id="pic" scope="page" class="picDeal.PicRgb">
</jsp:useBean> 你的pic 这个类不可用。因为你还加上
Java代码
1. <bean:define id="picList" name="pic" property="findXXX"></bean:define>
<bean:define id="picList" name="pic" property="findXXX"></bean:define>
其中: PicRgb 这个类的方法名要不能带参数,默认为getXXX(). 其中XXX的第一个单词要大写。因为这是jsp:useBean 约定写法。
demo.jsp:
Java代码
1. <jsp:useBean id="stockDao" class="dao.StockDao" scope="page"></jsp:useBean>
2. <bean:define id="stockList" name="stockDao" property="findAllStock"></bean:define>
3. <body>
4. <center>
5. insert
6. <html:form action="/insert.do">
7. id: <html:text property="id"></html:text>
8. sname: <html:text property="sname"></html:text>
9. price: <html:text property="price"></html:text>
10. acount: <html:text property="acount"></html:text>
11. <html:submit>submit</html:submit>
12. </html:form>
13.
14. <table border='1'>
15. <tr>
16. <td>
17. id
18. </td>
19. <td>
20. sname
21. </td>
22. <td>
23. price
24. </td>
25. <td>
26. acount
27. </td>
28. </tr>
29. <logic:iterate id="stock" name="stockList">
30. <tr>
31. <td>
32. <bean:write name="stock" property="id" />
33. </td>
34. <td>
35. <bean:write name="stock" property="sname" />
36. </td>
37. <td>
38. <bean:write name="stock" property="price" />
39. </td>
40. <td>
41. <bean:write name="stock" property="acount" />
42. </td>
43. </tr>
44. </logic:iterate>
45. </table>
46. </center>
47.
48. </body>
49. </html:html>
<jsp:useBean id="stockDao" class="dao.StockDao" scope="page"></jsp:useBean>
<bean:define id="stockList" name="stockDao" property="findAllStock"></bean:define>
<body>
<center>
insert
<html:form action="/insert.do">
id: <html:text property="id"></html:text>
sname: <html:text property="sname"></html:text>
price: <html:text property="price"></html:text>
acount: <html:text property="acount"></html:text>
<html:submit>submit</html:submit>
</html:form>
<table border='1'>
<tr>
<td>
id
</td>
<td>
sname
</td>
<td>
price
</td>
<td>
acount
</td>
</tr>
<logic:iterate id="stock" name="stockList">
<tr>
<td>
<bean:write name="stock" property="id" />
</td>
<td>
<bean:write name="stock" property="sname" />
</td>
<td>
<bean:write name="stock" property="price" />
</td>
<td>
<bean:write name="stock" property="acount" />
</td>
</tr>
</logic:iterate>
</table>
</center>
</body>
</html:html>
Java代码
1. public class StockDao {
2. private Access access;
3. // private Stock stock;
4. private ResultSet rs;
5. private Connection conn;
6. public static void main(String[] args) {
7. System.out.println(new StockDao().getFindAllStock());
8.
9. }
10.
11. public List getFindAllStock(){
12. access=new Access();
13. List list = new ArrayList();
14. String sql="select * from stock";
15. System.out.println(sql);
16. try {
17. rs=access.query(sql);
18. while(rs.next()){
19. Stock stock=new Stock();
20. stock.setId(rs.getString(1));
21. stock.setSname(rs.getString(2));
22. stock.setPrice(rs.getString(3));
23. stock.setAcount(rs.getString(4));
24. list.add(stock);
25. }
26. } catch (SQLException e) {
27. // TODO Auto-generated catch block
28. e.printStackTrace();
29. }
30.
31. return list;
32. }
展开阅读全文