Latest Tutorials| Questions and Answers|Ask Questions?|Site Map




Have Programming Question? Ask it here!
 
 
 


HarithaAdapa
again java
1 Answer(s)      3 years and 9 months ago
Posted in : Java Beginners


i will ask


iam using servlets ,in one program my requirement is if there is a image(if u like) in anywhere and we browse that image and it is store in u r account like orkut.
my doubt is if the image is stored in database it is not the good idea. my requirement is the image is stored in special folder and the path is stored in database. when sometime one image is displayed in u r account how it is possible.

Some friend has sent this code:

please observe the code:



)page.jsp:
<%@ page language="java" %>
<HTML>
<HEAD><TITLE>Display file upload form to the user</TITLE></HEAD>
<BODY> <FORM ENCTYPE="multipart/form-data" ACTION="../UploadImage" METHOD=POST>
<br><br><br>
<center>
<table border="0" bgcolor=#ccFDDEE>
<tr><center><td colspan="2" align="center"><B>UPLOAD THE FILE</B><center></td>
</tr>
<tr><td colspan="2" align="center">&nbsp;</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">&nbsp;</td>
</tr>
<tr><td colspan="2" align="center"><input type="submit" value="Send File"> </td>
</tr>
<table>
</center>
</FORM>
</BODY>
</HTML>

2)UploadImage.java:
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();
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;
saveFile="C:/UploadedFiles/"+saveFile;
File f = new File(saveFile);
FileOutputStream fileOut = new FileOutputStream(f);
fileOut.write(dataBytes, startPos, (endPos - startPos));
fileOut.flush();
fileOut.close();
out.println(saveFile);
Connection connection = null;
String connectionURL = "jdbc:mysql://localhost:3306/test";;
ResultSet rs = null;
PreparedStatement psmnt = null;
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
connection = DriverManager.getConnection(connectionURL, "root", "root");
psmnt = connection.prepareStatement("insert into file(file) values(?)");
psmnt.setString(1,f.getPath() );
int s = psmnt.executeUpdate();
if(s>0) {
System.out.println("Uploaded successfully !");
}
else{
System.out.println("unsucessfull to upload file.");
}
}
catch(Exception e){e.printStackTrace();}
}
}
}


my problem is


it is not working friend

pls check it fast .i need it

pls help me.

Advertisement
View Answers

July 23, 2010 at 5:18 PM


Hi Friend,

Have you got any error? Please specify it.

Thanks


Related Tutorials/Questions & Answers:
again java - Java Beginners
again java  i will ask iam using servlets ,in one program my requirement is if there is a image(if u like) in anywhere and we browse that image and it is store in u r account like orkut. my doubt is if the image is stored
again java - Java Beginners
again java  Hi, i will ask iam using servlets ,in one program my requirement is if there is a image(if u like) in anywhere and we browse that image and it is store in u r account like orkut. my doubt is if the image
Advertisements
java again - Date Calendar
java again  I can't combine your source code yesterday, can you help me again. My problem is how we get result jtextfield2 from if jtexfield1 we enter(jTextfield keypressed) then the result out to jTextfield2, This my jFrame
hi again - Java Beginners
/java/thread/thread-creation.shtml code after changing.. import java.io.
Hi .Again me.. - Java Beginners
Hi .Again me..  Hi Friend...... can u pls send me some code on JPanel.. JPanel shoul have 1pic 1RadioButton .. Like a Voter List...://www.roseindia.net/java/example/java/swing/ Thanks.  I am sending running code
matching database again - Java Beginners
matching database again  Dear experts, So happy I get through this ask a question page again. Thank God. I want to say "A BIG THANK YOU" for helping me about the matching codes. It is working now after fine tuning
Read data again - Java Beginners
Read data again  sir, i still hav a problem,first your code will be change like this : in netbeans out message error 5. Can you help me again. My database like my question before.Can you fix and find the problem in my code
Read data again - Java Beginners
Read data again  Hey, i want to ask again about how to read data from txt, My DB: kd_div varchar(15), nm_div varchar(30), dep varchar(25), jab varchar(35), cab varchar(15), ket varchar(30) My data in txt file is://i
doesnt run again - Java Beginners
the soltion   Hi I am sending u again the code, this code run in my
Hi..Again Doubt .. - Java Beginners
Hi..Again Doubt ..  Thank u for ur Very Good Response...Really great.. i have completed that.. If i click the RadioButton,,ActionListenr should get call. It should add to the MS Acess table..Plz check this out.... hope u ill
call frame again - Java Beginners
read from jbutton1 in FrameA to FrameB,then i write "JAVA" in Jtextfield1(FrameB),then i click jbutton1 in FrameB. "JAVA" is a word i'am write in Jtexfield1
help again plz sorry - Java Beginners
help again plz sorry  Thanks for giving me thread code but i have a question this code is comletelly right and i want to make it runs much faster.... http://www.roseindia.net/java/thread/Java-Multithreading.shtml Thanks
There and Back Again
There and Back Again       The weblog of Joshua Eichorn, AJAX, PHP and Open Source Read full Description
again with xml - XML
again with xml  hi all i am a beginner in xml so pls give me the details regarding the methods used in it. wat will return the methods getElementsByTagName(), getChildNodes(), getParentNode(), nextSibling(),etc and when
Script on the page used too much memory. Reload to enable script again.
Script on the page used too much memory. Reload to enable script again.  Using a java script to generate the dynamic report. If page open the full... to enable script again". After getting this error other pages also not working
the last data entered into database is getting stored again after refreshing
the last data entered into database is getting stored again after refreshing  hey all i made a shout box using php and mysql but the last data entered into the DB is getting retrieved again as i refresh the page.. even
submitting corect user and passowrd, it again comes on login.jsp - Struts
submitting corect user and passowrd, it again comes on login.jsp  i am submit username and password in login.jsp, after submit correct username and password, it again come in login.jsp. Here some files: Struts.xml
this code will be problem it display the error again send jsp for registration form
this code will be problem it display the error again send jsp for registration form  I AM ENTERING THE DETAILS OFTER IT DISPLAY THE ERROR PLEASE...;/option> <option value="C#">C#</option> <option value="Java
this code will be problem it display the error again send jsp for registration form
this code will be problem it display the error again send jsp for registration form  I AM ENTERING THE DETAILS OFTER IT DISPLAY THE ERROR PLEASE...;/option> <option value="C#">C#</option> <option value="Java
how to prevent no from unroundin off on clicking text box again in JSP and Jquery
how to prevent no from unroundin off on clicking text box again in JSP... reclick that text box ,It shows the same no (unrounded no) again , Though on clicking outside It rounds off again, How can i prevent it from un-rounding off
how to prevent no from unroundin off on clicking text box again in JSP and Jquery
how to prevent no from unroundin off on clicking text box again in JSP... reclick that text box ,It shows the same no (unrounded no) again , Though on clicking outside It rounds off again, How can i prevent it from un-rounding off
Struts Login Validation. In This code setter of login page is called only one time again it doesnt call it. Why..?
Struts Login Validation. In This code setter of login page is called only one time again it doesnt call it. Why..?  **struts.xml** <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software
Java XStream
Java XStream       XStream is a simple library used to serialize the objects to XML and back again into the objects. Features of the XStream APIs XStream provides better
Difference between Java and C++
Difference between Java and C++ Java is an Object Oriented Programming(OOPs) language, developed by James Gosling 1992. Important feature of java... that the code that run on one platform need not required to compile again
Java: Garbage Collection
Java: Garbage Collection Garbage collection is the process... them available again. In contrast to manual deallocation that is used by many languages, eg C and C++, Java automates this error-prone process. Manual
Java garbage collection
Java garbage collection  Can an unreachable object become reachable again
Loops
instructions to repeat the code itself again and again until certain conditions are met... to repeat again and again until the result found or the number of times... to $ Counting; d. go to sep a. and run this script again with the new value
java program - Java Beginners
java program  1.write a program to show traffic signal using multithreading. 2.Except an integer from 1-12 and display corresponding month... the user to enter it again
java - Java Interview Questions
java  Can unreachable object become reachable again?   Hi friend, Yes,an unreachable object may become reachable again. The garbage... information : http://www.roseindia.net/java/example/java/util/ Thanks
Java
Java  Again it gives the following error. java.lang.ClassNotFoundException: com.mysql.jdbc.Driver is there any necessaity to install anything
Java
Java  again same error i got. send me another link
Java Code - Java Beginners
Java Code  Code an application with a GUI that provides 3 menu... again. ? If the user selects the second menu option.... Then, it displays the menu again. ? If the user selects the third menu
java - Java Beginners
java  hi all, In Java swings, I created a array of JPanel and added... the panel positions will change but when i rum again there is old positions only... i run again.. pls do this favour...thanks in advance
java - Java Beginners
java  dear sir, plz give me a java code where we copy a files from a folder and paste in a single file by zip, and again open the all files in a single file
Java Program - Java Beginners
Java Program  Code an application using a GUI called Week Four... saved. Then, it displays the menu again. ? If the user selects..., and phone number. Then, it displays the menu again
Java Program - Java Beginners
Java Program  Code an application called Week Four Program.... Then, it displays the menu again. ? If the user selects the second menu... number. Then, it displays the menu again. ? If the user selects
Java
Java  Again i got the following errors.is succesfully compiled.but... Formdata1.java C:\work1>java Formdata1 Exception in thread "main...(ClassLoader.java:319) C:\work1>javac Formdata1.java C:\work1>java Formdata1
java - Java Beginners
\Java\jdk1.5.0\bin (Instead of putting C:\Program Files\Java\jdk1.5.0 again you...\Java\jdk1.5.0\bin; (Instead of putting C:\Program Files\Java\jdk1.5.0 again you...java   i have installed java 1.5 with tomcate apache server. and i
java swing jdbc - Java Beginners
java swing jdbc  hello, i want to populate the data(ms access) to the jtable component and want to perform some operations on it like add,edit,delete. Again i want to store the updated data to the database. can you help me out
plot a graph in Java
plot a graph in Java   I need to plot a graph in Java but the samples... need to plot them as I receive them. This requires calling repaint() again and again which redraws the entire area on the JPanel thereby overwriting the previous
about JAVA - Java Beginners
ERROR and ask the user again to enter the number of inputs. i don't know how to do... to try again, if they type Y they will be asked again to enter number of inputs...; } } } } } ------------------------------------------- Read for more information, http://www.roseindia.net/java
java
java  diff bt core java and java
Java source file - Java Beginners
Java source file  i create a file.java just now but mistakenly i... to retrieve the source file again with that class file?  Hi Friend, You can use Java Decompiler tools like "Cavaj" to decompile the .class file
java
java  what is java
 

 

 

DMCA.com