CodeCharge Studio
search Register Login  

Web Reporting

Visually create Web Reports in PHP, ASP, .NET, Java, Perl and ColdFusion.
CodeCharge.com

YesSoftware Forums -> CodeCharge Studio -> Java

 image refused to be uploaded into the database

Print topic Send  topic

Author Message
fredrick

Posts: 4
Posted: 03/07/2010, 4:39 PM

Please help. this code returns blank page instead of uploading the picture in the database
Please what do i do. it's supposed to display IMAGE SUCCESSFUL if the image is inserted into the database.
Please sir help me check if the problem is from <input type="file" name="file" id="file">
because my table datatype is blob with file as the column name.


thanks and God bless








<html>

<head><title>Image Upload</title></head>

<body>
<form action="/form2/UploadImage" method="post" enctype="multipart/form-data"
name="productForm" id="productForm"><br><br>
<table width="400px" align="center" border=0 style="background-color:ffeeff;">
<tr>
<td align="center" colspan=2 style="font-weight:bold;font-size:20pt;">
Image Details</td>
</tr>

<tr>
<td align="center" colspan=2> </td>
</tr>

<tr>
<td>Image Link: </td>
<td>
<input type="file" name="file" id="file">
<td>
</tr>

<tr>
<td></td>
<td><input type="submit" name="Submit" value="Submit"></td>
</tr>
<tr>
<td colspan="2"> </td>
</tr>

</table>
</form>
</body>

</html>

















import java.io.*;
import java.sql.*;
import java.util.*;
import java.text.*;
import java.util.regex.*;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.*;

import javax.servlet.*;
import javax.servlet.http.*;

public class UploadImage extends HttpServlet{
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
System.out.println("request: "+request);
if (!isMultipart) {
System.out.println("File Not Uploaded");
} else {
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
List items = null;

try {
items = upload.parseRequest(request);
System.out.println("items: "+items);
} catch (FileUploadException e) {
e.printStackTrace();
}
Iterator itr = items.iterator();
while (itr.hasNext()) {
FileItem item = (FileItem) itr.next();
if (item.isFormField()){
String name = item.getFieldName();
System.out.println("name: "+name);
String value = item.getString();
System.out.println("value: "+value);
} else {
try {
String itemName = item.getName();
Random generator = new Random();
int r = Math.abs(generator.nextInt());

String reg = "[.*]";
String replacingtext = "";
System.out.println("Text before replacing is:-" + itemName);
Pattern pattern = Pattern.compile(reg);
Matcher matcher = pattern.matcher(itemName);
StringBuffer buffer = new StringBuffer();

while (matcher.find()) {
matcher.appendReplacement(buffer, replacingtext);
}
int IndexOf = itemName.indexOf(".");
String domainName = itemName.substring(IndexOf);
System.out.println("domainName: "+domainName);

String finalimage = buffer.toString()+"_"+r+domainName;
System.out.println("Final Image==="+finalimage);

File savedFile = new File("C:/Tomcat 6.0/webapps/form2/image/"+"images\\"+finalimage);

item.write(savedFile);
out.println("<html>");
out.println("<body>");
out.println("<table><tr><td>");
out.println("<img src=images/"+finalimage+">");
out.println("</td></tr></table>");

Connection conn = null;
String url = "jdbc:mysql://localhost:3306/";
String dbName = "milk";
String driver = "com.mysql.jdbc.Driver";
String username = "root";
String userPassword = "root";
String strQuery = null;
String strQuery1 = null;
String imgLen="";

try {
System.out.println("itemName::::: "+itemName);
Class.forName(driver).newInstance();
conn = DriverManager.getConnection(url+dbName,username,userPassword);
Statement st = conn.createStatement();
strQuery = "insert into testimage1 set file='"+finalimage+"'";
int rs = st.executeUpdate(strQuery);
System.out.println("Query Executed Successfully++++++++++++++");
out.println("image inserted successfully");
out.println("</body>");
out.println("</html>");
} catch (Exception e) {
System.out.println(e.getMessage());
} finally {
conn.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
}

_________________
THANKS
View profile  Send private message

Add new topic Subscribe to topic   


These are Community Forums for users to exchange information.
If you would like to obtain technical product help please visit http://support.yessoftware.com.

MS Access to Web

Convert MS Access to Web.
Join thousands of Web developers who build Web applications with minimal coding.

CodeCharge.com

Home   |    Search   |    Members   |    Register   |    Login


Powered by UltraApps Forum created with CodeCharge Studio
Copyright © 2003-2004 by UltraApps.com  and YesSoftware, Inc.