0

我创建了一个包含以下条目的数据库

File_data     longvarchar
File_date     date

我们正在尝试在upload.jsp 中将文件插入数据库,但是当我单击sendfile 按钮时,它显示一个空白页并且文件没有保存在数据库中。

<%@ page import="java.io.*,java.sql.*,java.util.*,java.text.*,java.text.SimpleDateFormat" %>
<html>
<%
int val =0;
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);
String saveFile = file.substring(file.indexOf("filename=\"") + 10);
System.out.println("saveFile=" + saveFile);
saveFile = saveFile.substring(saveFile.lastIndexOf("\\")+ 1,saveFile.indexOf("\""));
System.out.println("saveFile" + saveFile);
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;

FileOutputStream fileOut = new FileOutputStream(saveFile);
fileOut.write(dataBytes, startPos, (endPos - startPos));
 %>

 <%
Connection con=null;
PreparedStatement pstatement = null;
String line = null;
String value=null;
String url = "jdbc:derby://localhost:1527/";
String dbName = "CustomerDB";
String userName = "admin"; 
String password = "admin";
try{
StringBuilder contents = new StringBuilder();
BufferedReader input = new BufferedReader(new FileReader(saveFile));
while (( line = input.readLine()) != null){
contents.append(line);
}
value = contents.toString();
System.out.println("Value:"+value);
Class.forName("oracle.jdbc.driver.OracleDriver");
con = DriverManager.getConnection(url+dbName,userName,password);
java.util.Date now = new java.util.Date();
String DATE_FORMAT = "yyyy-MM-dd hh:mm:ss";
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
String strDateNew = sdf.format(now) ;

String queryString = "INSERT INTO file_tbl set file_data='"+value+"',file_date='"+strDateNew+"'";

//out.println(queryString);

pstatement=con.prepareStatement(queryString);


val = pstatement.executeUpdate();

 if(val>0)
 {
 %>
 <br><br>
  <b>File <% out.println(saveFile); %> has been uploaded and inserted into Database at <%=strDateNew%>.</b>
 <%
 }


 }
 catch(Exception e)
  {}
 }
 %>
 </html>

我没有得到正在发生的事情,因为它没有向我显示任何错误。所以请帮助我如何解决它。

4

1 回答 1

0
<?xml version="1.0" encoding="ISO-8859-1" ?>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
 pageEncoding="ISO-8859-1"%>
 <%@ page  import="java.io.*,java.sql.*,java.util.*,java.text.*,java.text.SimpleDateFormat" %>    
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
 <head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Insert title here</title>
</head>
<body>


 <%
 int val =0;
String contentType = request.getContentType();
if ((contentType != null) && (contentType.indexOf("multipart/form-data") >= 0)) {
DataInputStream in = new DataInputStream(request.getInputStream());
int formDataLength = request.getContentLength();
out.println(formDataLength);
 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);
String saveFile = file.substring(file.indexOf("filename=\"") + 10);
System.out.println("saveFile= " + saveFile);
saveFile = saveFile.substring(saveFile.lastIndexOf("\\")+ 1,saveFile.indexOf("\""));
System.out.println("saveFile" + saveFile);
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;

 FileOutputStream fileOut = new FileOutputStream(saveFile);
 fileOut.write(dataBytes, startPos, (endPos - startPos));
  %>

 <%
 String line = null;
 String value=null;
 try{
 StringBuilder contents = new StringBuilder();
 BufferedReader input = new BufferedReader(new FileReader(saveFile));
 while (( line = input.readLine()) != null){
  contents.append(line);
 }
 value = contents.toString();
 System.out.println("Value: "+value);
Class.forName("oracle.jdbc.driver.OracleDriver");
 Connection con=DriverManager.getConnection("jdbc:oracle:thin:@//localhost:1521/xe","system","vyas");
 java.util.Date now = new java.util.Date();
 String DATE_FORMAT = "yyyy-MM-dd hh:mm:ss";
 SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
 String strDateNew = sdf.format(now) ;
 System.out.println(strDateNew);


 Statement stmt=con.createStatement();
 System.out.println("Run");
 stmt.executeUpdate("insert into userstorage (data,date) values('"+ value +"','"+        strDateNew +"')");
 System.out.println("query successfully run");

 }
 catch(Exception e)
{
 System.out.println(e.getMessage());
}
 }
 %>

 </body>
 </html>
于 2013-04-11T10:34:42.417 回答