How to upload and save a file in a folder
View Answers
January 9, 2009 at 5:54 AM
Hi Friends,
I need that code using servlets and jsp.....
Please let me know the code
Thanks in advance
January 9, 2009 at 11:40 PM
Hi friend,
Code to help in solving the problem :
<html>
<head><title>Image Upload</title></head>
<body>
<form action="/example/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>
January 9, 2009 at 11:47 PM
"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();
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);
}
January 9, 2009 at 11:48 PM
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:/apache-tomcat-6.0.16/
webapps/example/"+"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 = "test";
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 testimage set image='"+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
January 10, 2009 at 5:52 AM
Hi friend,
Thanks for your code....
Do i need any jarfiles to be placed in lib?
Java program when i tried to compile is showing some errors..
Thanks in advance
July 11, 2012 at 8:10 PM
you need to add the following jar to lib folder:
commons-io-1.3.1-osgi.jar
commons-fileupload-1.2.jar
ojdbc14.jar
Ads
Related Tutorials/Questions & Answers:
Advertisements
upload and save the file
upload and
save the file
upload and
save a
file from a computer to specific location of remote computer using jsp
how to download a file from a folder??
how to download a
file from a
folder?? i can
upload any kind of files to a
folder, and only the path of the
file is saved into the database, now
how a client can download the
file from my
folder. pls provide me the jsp code
How to save file in vi editor?
How to
save file in vi editor? Hi,
I am trying to find commands... the commands?
Thanks
Hi,
Here are the details:
How to
save file... :w to
save the
file
Type :wq to
save and quit
Type :
q! to quit without saving
Struts File Upload and Save
Struts
File Upload and
Save
 ... code illustrating
how to
save the
file on the server . Now, the current...
Upload</html:link>
<br>
Example shows you
how to
Upload File with Struts
How to upload and download file in hadoop?
How to
upload and download
file in hadoop? Hi,
I am trying to learn to
upload the
file on the Hadoop HDFS and then download the same
file for learning the process.
How to
upload and download
file in hadoop?
What
How to upload file on FTP Server
How to
upload file on FTP Server In my application there is a requirement of uploading the
file on FTP server.
How to
upload file on FTP Server... is good and can be used in Java applications.
Read FTP
File Upload in Java for more
How to upload file using JSP?
How to
upload file using JSP? Hi all,
I m the beginner in JSP, I want to
upload file on server in specific
folder.
1)page.jsp...
file upload form to the user</TITLE></HEAD>
<
How to save JPanel as gif or jpeg file
How to
save JPanel as gif or jpeg file i draw polygons , rectangles, lines on jpanel using draw line method drawpolygon methods etc, now i want
save the data of jpanel which is drawn by me(lines,polygons) as a gif or jpeg
file
image save to folder in java
image
save to
folder in java Hi,
I am working with java. In my application i want to give facility to user to add and change image. I use open...
folder and that path stored into database, for furhter retrivation.
Please guide me
Full path of image to save it in a folder
Full path of image to
save it in a folder Sir ,I am trying to
upload a
file but only sends
file name as parameter.
I want a code that would help me... of either JDBC or JPA, whichever is known to you.
2. saving the
file in a
folder
How to save JPanel as gif or jpeg file
How to
save JPanel as gif or jpeg file i draw polygons , rectangles, lines on jpanel using draw line method drawpolygon methods etc, now i want
save the data of jpanel which is drawn by me(lines,polygons) as a gif or jpeg
file
How to Increase Upload File Size Limit in php
How to Increase
Upload File Size Limit in php Increase
Upload File.... But the default
file upload size settings restrict you to
upload large files. PHP default setting allowed only 2MB
file upload size.
So if you want to
upload
Struts 2 File Upload
you will learn
how to write program in
Struts 2 to
upload the
file... will learn
about the code to
upload file on the server.
How File Upload Works... to
save the uploaded
file
try
{
String fullFileName = "c:/
upload
image upload in webapp/upload folder
image
upload in webapp/
upload folder sir i want to store
upload image in my project directory WebApp/
Upload_image/
pls send the jsp servlet code
when i
upload the image one error found
"system cannot found the specified path
How to upload a large excel file - JSP-Servlet
How to
upload a large excel
file Dear sir ,
How to
upload a large excel
file and
how to read a that large excel
file or
how to get a each...
upload a large
file ,if i
upload a small
file it works fine .Where my code