2012-03-13 13 views
0

jspを使用してGoogle Cloud Sqlに画像を挿入するには、[JSP Insert Image] [1]に従っていましたが失敗しました。私は以下 GoogleクラウドSql - 画像の挿入に失敗しました

が私のコードである java.lang.NoClassDefFoundError: java.io.FileOutputStream is a restricted class.Please see the Google App Engine developer's guide for more details.を得た:はUpload.jsp

Page.JSP

<%@ page language="java" %> 
<HTML> 
<HEAD><TITLE>Display file upload form to the user.</TITLE></HEAD> 
<BODY> <FORM ENCTYPE="multipart/form-data" ACTION="upload.jsp" METHOD=POST> 
<br><br><br> 
<center> 
<table border="0" bgcolor=#ccFDDEE> 
<tr> 
<center> 
<td colspan="2" align="center"><B>UPLOAD THE FILE IN JSP PAGE</B><center></td> 
</tr> 
<tr> 
<td colspan="2" align="center"> </td> 
</tr> 
<tr> 
<td><b>Choose the file To Upload:</b></td> 
<td><INPUT NAME="file" TYPE="file"></td> 
</tr> 
<tr> 
<td colspan="2" align="center"> </td> 
</tr> 
<tr> 
<td colspan="2" align="center"><input type="submit" value="Send File"> </td> 
</tr> 
<table> 
</center> 
</FORM> 
</BODY> 
</HTML> 

<%@ page import="java.io.*" %> 
<%@ page import="java.sql.*" %> 
<%@ page import="java.util.zip.*"%> 
<%@ page import="com.google.appengine.api.rdbms.AppEngineDriver" %> 
<% 
String saveFile=""; 
String contentType = request.getContentType(); 
if((contentType != null)&&(contentType.indexOf("multipart/form-data") >= 0)){ 
DataInputStream in = new DataInputStream(request.getInputStream()); 
int formDataLength = request.getContentLength(); 
byte dataBytes[] = new byte[formDataLength]; 
int byteRead = 0; 
int totalBytesRead = 0; 
while(totalBytesRead < formDataLength){ 
byteRead = in.read(dataBytes, totalBytesRead,formDataLength); 
totalBytesRead += byteRead; 
} 
String file = new String(dataBytes); 
saveFile = file.substring(file.indexOf("filename=\"") + 10); 
saveFile = saveFile.substring(0, saveFile.indexOf("\n")); 
saveFile = saveFile.substring(saveFile.lastIndexOf("\\") + 1,saveFile.indexOf("\"")); 
int lastIndex = contentType.lastIndexOf("="); 
String boundary = contentType.substring(lastIndex + 1,contentType.length()); 
int pos; 
pos = file.indexOf("filename=\""); 
pos = file.indexOf("\n", pos) + 1; 
pos = file.indexOf("\n", pos) + 1; 
pos = file.indexOf("\n", pos) + 1; 
int boundaryLocation = file.indexOf(boundary, pos) - 4; 
int startPos = ((file.substring(0, pos)).getBytes()).length; 
int endPos = ((file.substring(0, boundaryLocation)).getBytes()).length; 
File ff = new File(saveFile); 
FileOutputStream fileOut = new FileOutputStream(ff); 
fileOut.write(dataBytes, startPos, (endPos - startPos)); 
fileOut.flush(); 
fileOut.close(); 
%><Br><table border="2"><tr><td><b>You have successfully upload the file:</b> 
<% out.println(saveFile);%></td></tr></table> 
<% 

ResultSet rs = null; 
PreparedStatement psmnt = null; 
FileInputStream fis; 
try{ 
Connection c = null; 
c = DriverManager.getConnection("jdbc:google:rdbms://smartposterdb:uploadimagedb/guestbook"); 
DriverManager.registerDriver(new AppEngineDriver()); 
File f = new File(saveFile); 
psmnt = c.prepareStatement("insert into photo(image) values(?)"); 
fis = new FileInputStream(f); 
psmnt.setBinaryStream(1, (InputStream)fis, (int)(f.length())); 
int s = psmnt.executeUpdate(); 
if(s>0){ 
System.out.println("Uploaded successfully !"); 
} 
else{ 
System.out.println("Error!"); 
} 
} 
catch(Exception e){e.printStackTrace();} 
} 
%> 



    [1]: http://www.roseindia.net/answers/viewqa/HTML/18225-Adding-image-to-database-through-jsp-or-HTML-page-while-adding-only-image-should-show-.html 
+2

roseindia.netの "チュートリアル"は読んではいけません。彼らは**悪い習慣**で過負荷です。この特定のコードに関する問題は、ここで概要を説明しています:http://stackoverflow.com/questions/5038798/uploading-of-pdf-file/5041420#5041420 – BalusC

答えて

0

まあ、まず、Google App Engineが許容周りの制限のセットを持っていますファイルシステムに対する操作。 http://en.wikipedia.org/wiki/Google_App_Engine#Restrictions

2番目に、ファイルをSQL BLOB/CLOBオブジェクトにバイト配列として書き込むのではなく(Google Cloud SQLで使用できると仮定しています)あなたはおそらくbase64エンコード/デコードする必要がありますが、読み込みとSQLからの書き込みの場合

関連する問題