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 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"> </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>
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
how to compile,run and where to see o/p please tell me step by step procedure.
pls help me.
View Answers
Related Tutorials/Questions & Answers:
again java - Java Beginnersagain 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 Beginnersagain 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 Calendarjava 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 me.. - Java BeginnersHi .
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 Beginnersmatching 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 BeginnersRead 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 BeginnersRead 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
Hi..Again Doubt .. - Java BeginnersHi..
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 Beginnershelp
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 DescriptionADS_TO_REPLACE_1
ModuleNotFoundError: No module named 'again'ModuleNotFoundError: No module named '
again' Hi,
My Python program is throwing following error:
ModuleNotFoundError: No module named '
again'
How to remove the ModuleNotFoundError: No module named '
again'
ModuleNotFoundError: No module named 'again'ModuleNotFoundError: No module named '
again' Hi,
My Python program is throwing following error:
ModuleNotFoundError: No module named '
again'
How to remove the ModuleNotFoundError: No module named '
again'
again with xml - XMLagain 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
ModuleNotFoundError: No module named 'n-again'ModuleNotFoundError: No module named 'n-
again' Hi,
My Python...-
again'
How to remove the ModuleNotFoundError: No module named 'n-
again... to install padas library.
You can install n-
again python with following command
Drop Down Reloads again in IE..How to prevent this?Drop Down Reloads
again in IE..How to prevent this? Hi i was using two drop down box..One for Displaying date followed by another for Dispalying...? Its purely JavaScript and HTML page..Im not uing this concept in
Java or any
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 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.... go to sep a. and run this script
again with the new value of $ Counting; ADS
java program - Java Beginnersjava 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 JavaScript blink method
JavaScript blink method
Some times you need that some of the html text blinks
on the browser
again and
again without using any kind of picture or image so to
add
java - Java Interview Questionsjava 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 Code - Java BeginnersJava 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 Beginnersjava 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