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

» JWorld@TW » Java 新手區  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友    訂閱主題
reply to postflat modego to previous topicgo to next topic
本主題所含的標籤
無標籤
作者 檔案上傳
wanyu





發文: 1
積分: 0
於 2005-07-27 14:41 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
請教各位先進:
參考社群中的文章想要作一個上傳圖檔存到DB,先照著文章中的程式碼跑一次,卻發生一些錯誤,不知道問題在哪?
參考文章:
http://www.javaworld.com.tw/jute/post/view?bid=6&id=19782&sty=3&age=0&tpg=1&ppg=1#19782

我猜其中的一部份原因是MultipartRequest這個新類別,我也參考了論壇中的先進們的指示至 http://www.servlets.com/cos/ 下載了API,並將cos.jar解壓縮至幾個目錄,會這麼做的原因是大家的說法都不同,所以我都複製了,但還是有問題,只好請教各位先進了!
我開發的環境是Win2000Pro+Oracle10g+Jdeveloper10g+oc4j10g+J2SDK1.4.2_08
另外,錯誤也產生在找不到import的類別,不是很了解oracle.jdbc.*及oracle.jdbc.driver.*系統會依什麼規則找?難道還要再裝oracle jdbc driver嗎?

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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package mypackage3;
import javax.servlet.*;
import javax.servlet.http.*;
//import java.io.PrintWriter;
//import java.io.IOException;
import java.io.*;
import java.util.*;
import java.sql.*;
import java.lang.*;
import oracle.jdbc.*;
import oracle.jdbc.driver.*;
import com.oreilly.servlet.MultipartRequest;
 
public class Servlet1 extends HttpServlet 
{
  private static final String CONTENT_TYPE = "text/html; charset=Big5";
  final String sDriver = "oracle.jdbc.driver.OracleDriver";
  final String sURL = "jdbc:oracle:thin:@localhost:1521:ORCL";
  final String USERNAME = "user";
  final String PASSWORD = "pwd";
  final String saveDirectory = "."+System.getProperty("file.separator")+"UploadTemp"+System.getProperty("file.separator");
  //int maxPostSize = 5 * 1024 * 1024 ;
  int maxPostSize = 640 * 480 ;
  Vector vdata;
  MultipartRequest multi;
  public Servlet1()
  {
  }
  public void init() throws ServletException
  {
    //super.init(config);
    vdata = new Vector();
  }
 
  public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
  {
    //response.setContentType(CONTENT_TYPE);
    //PrintWriter out = response.getWriter();
    //out.println("<html>");
    //out.println("<head><title>Servlet1</title></head>");
    //out.println("<body>");
    //out.println("<p>The servlet has received a GET. This is the reply.</p>");
    //out.println("</body></html>");
    //out.close();
    doPost(request, response);
  }
 
  public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
  {
    //response.setContentType(CONTENT_TYPE);
    //PrintWriter out = response.getWriter();
    //out.println("<html>");
    //out.println("<head><title>Servlet1</title></head>");
    //out.println("<body>");
    //out.println("<p>The servlet has received a POST. This is the reply.</p>");
    //out.println("</body></html>");
    //out.close();
    String msg = "";
    Connection con = null;
    PreparedStatement prestmt = null;
    String FileDescription[] = {null,null,null};
    try
    {
      Class.forName(sDriver).newInstance();
      con = DriverManager.getConnection(sURL, USERNAME, PASSWORD);
      con.setAutoCommit(false);
      File fp = new File(saveDirectory);
      if(!fp.exists())
        fp.mkdir();
      prestmt = con.prepareStatement("INSERT INTO MYTABLE(DOCID,MIMETYPE) VALUES(?,?)");
      multi = new MultipartRequest(request , saveDirectory , maxPostSize );
      Enumeration filesname = multi.getFileNames();
      while (filesname.hasMoreElements())
      {
        String name = (String) filesname.nextElement();
        String FileName = fp_isNull(multi.getFilesystemName(name));
        if(!FileName.equals(""))
        {
          File f = multi.getFile(name);
          String ContentType = fp_isNull(multi.getContentType(name));
          FileInputStream fis = new FileInputStream(f);
          prestmt.setString(1, FileName);
          prestmt.setString(2, ContentType);
          prestmt.execute();
          Statement stmt = con.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE);
          ResultSet rs = stmt.executeQuery("SELECT CONTENTDATA FROM MYTABLE WHERE DOCID='"+FileName+"' FOR UPDATE");
          rs.next();
          oracle.sql.BLOB BB=(oracle.sql.BLOB )rs.getBlob(1);
          OutputStream blobOutputStream =BB.getBinaryOutputStream();
          byte[] buffer = new byte[1024];
          int nread = 0;
          while( (nread= fis.read(buffer)) != -1 )
            blobOutputStream.write(buffer, 0, nread); // Write to Blob
          blobOutputStream.flush();
          fis.close();
          blobOutputStream.close();
          stmt.close();
          f.delete();
        }
      }
      con.commit();
      fp.delete();
      msg = "Succeed ";
    }catch(Exception e)
    {
      try{con.rollback();}catch(Exception esql){}
      msg += "  Error : "+e;
      System.out.println("error : "+e.toString());
    }finally
    {
      if(prestmt!=null)
      {
        try{prestmt.close();}catch(Exception e){}
        prestmt = null;
      }
      if(con!=null)
      {
        try
        {
          con.setAutoCommit(true);
          con.close();
        }catch(Exception e){}
        con=null;
      }
    }try
    {
      response.sendRedirect("."+System.getProperty("file.separator")+".."+System.getProperty("file.separator")+"jsp2.jsp?message="+msg);
    }catch(IOException e){}
  }
  public void destroy()
  {
    
  }
  private String fp_isNull(Object ob)
  {
    String st;
    if(ob == null)
      st="";
    else
      st=ob.toString().trim();
    return st;
  }
}


Complier出現的錯誤訊息是
1
2
3
4
5
6
7
8
9
Project: C:\JDev10g\jdev\mywork\Exercise\Exercises\Exercises.jpr
  C:\JDev10g\jdev\mywork\Exercise\Exercises\src\mypackage3\Servlet1.java
    Error(10,1): cannot access directory oracle\jdbc; verify that directory isreachable from classpath and/or sourcepath
    Error(11,1): cannot access directory oracle\jdbc\driver; verify thatdirectory is reachable from classpath and/or sourcepath
    Error(12,28): cannot access class com.oreilly.servlet.MultipartRequest;file com\oreilly\servlet\MultipartRequest.class not found
    Error(25,3): class MultipartRequest not found in class mypackage3.Servlet1
    Error(71,19): class MultipartRequest not found in class mypackage3.Servlet1
    Error(88,22): cannot access class oracle.sql.BLOB; fileoracle\sql\BLOB.class not found
    Error(88,42): cannot access class oracle.sql.BLOB; fileoracle\sql\BLOB.class not found


browser edited on 2005-07-27 15:15
reply to postreply to post
話題樹型展開
人氣 標題 作者 字數 發文時間
1410 檔案上傳 wanyu 6194 2005-07-27 14:41
» JWorld@TW »  Java 新手區

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

JWorld@TW 本站商標資訊

Powered by Powerful JuteForum® Version Jute 1.5.8