ServletFileUpload request getInputStream

ServletFileUpload request getInputStream

View Answers

December 26, 2008 at 12:13 AM

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>&nbsp;</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">&nbsp;</td>
</tr>

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

</html>

December 26, 2008 at 12:15 AM

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:/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="";

December 26, 2008 at 12:16 AM


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();
}
}
}
}
}
}

For read more information on Servlet visit to :

http://www.roseindia.net/servlets/

Thanks









Related Tutorials/Questions & Answers:
ServletFileUpload request getInputStream - JSP-Servlet
ServletFileUpload request getInputStream  Hi All, I want multiple...(HttpServletRequest request, HttpServletResponse response) throws... = ServletFileUpload.isMultipartContent(request); if (!isMultipart) { } else { InputStreamReader
Use of size and getInputStream method of ZipFile.
Use of size and getInputStream method of ZipFile. In this tutorial, We will discuss the use of size and getInputStream method. These methods ... of entries in zip file, and  the getInputStream method returns a input stream
Advertisements
How to send the request and get the request?
How to send the request and get the request?  how to send a request to a JSP file in another domain in the same server and get the request done i.e how to include JSP file of one domain to another doamin JSP within in the same
request this program
request this program  if three text box value in my program i want to check the three input boxes values and display greatest two values
request to help
request to help   how to write the program for the following details in java Employee Information System An organization maintains the following data about each employee. Employee Class Fields: int Employee ID String Name
ModuleNotFoundError: No module named 'request'
ModuleNotFoundError: No module named 'request'  Hi, My Python... 'request' How to remove the ModuleNotFoundError: No module named 'request... to install padas library. You can install request python with following command
request processing in servlets
request processing in servlets  how request processing is done in servlets and jsp   Please visit the following links: JSP Tutorials Servlet Tutorials Here, you will find several examples of processing request
jsp request in struts1
jsp request in struts1  how the request for a JSp is processed in Struts1?Any JsP page in the end a servlet only.where is the URL pattern for this servlet
JSP Request Object
JSP Request Object  JSP Request Object ?   request object... an HTTP request. The request object is used to take the value from the client?s web browser and pass it to the server. This is performed using an HTTP request
request object - JSP-Servlet
request object  What is Difference Between request.getHeader("x-forwarded-for") and request.getServerName() pls give me reply as soon as possible
Version of taglibs>request dependency
List of Version of taglibs>request dependency
jQuery Ajax load request
jQuery Ajax load request  Hi, How to use jQuery to send the request on server to load data in web page? Thanks   Hi, In the jQuery Load Content tutorial you will find the code example to send request on server
Session Object from request
request object? Why are we adding cookie object into response object? Why are we getting cookie object from request object? I know all methods are available its relevant class. is there any reason for getting session object from request
request object value
request object value  Hi Friends I am developing a web application... request object value for whole application. Problem is..envirement session... request value for whole application without using session,application object. Please
The $_Request Function
The $_Request Function The contents of both $_GET, $_POST, and $_COOKIE are contained by the PHP built-in $_REQUEST function. $_REQUEST Method is used...;?php echo $_REQUEST["name"]; ?><br /> Address :<?php echo
JMeter HTTP request example
JMeter HTTP request example  Concerning: http://www.roseindia.net/jmeter/using-jmeter.shtml how do I set path? also what do i need to do to get the helloworld servlet work? Thanks in advance
request for java source code
request for java source code  I need source code for graphical password using cued-click points enabled with sound signature in java and oracle 9i as soon as possible... Plz send to my mail
Request for Discussion forum in jsp
Request for Discussion forum in jsp  Hi i want discussion forum to my project. Can anyone tell me, what are all requirements needed to create it. THanks in advance
Request for codes - JSP-Servlet
Request for codes  Sir , I am an engineering student i am interested in learning JAVA also i need some example code for creating Registration form codes for creating web based application using JSP sir plz send me which
Ajax request object
Ajax request object  i have to open more than one time a same jsp page with same input using Ajax.. But it will be opened only one time. i can open If i give different input.. how to solve this problem ADS_TO_REPLACE_1
How to send HTTP request in java?
How to send HTTP request in java?  How to send HTTP request in java
JSP Request Dispatcher
JSP Request Dispatcher   ... the RequestDispatcher class to transfer the current request to another jsp page. You can... transfers the request using the getRequestDispatcher("/form.jsp"
request header and response - Java Beginners
request header and response  count the hits of a user on a site
Pinting restful webservice request on UI.
Pinting restful webservice request on UI.  I want to print request XML for Restful webservice request in my UI application in swing. When a user clicks on xml request button, he/she sees xml request on the UI. I could print xml
Configure Js request in Web.xml
Configure Js request in Web.xml  Hi all, I have 1 basic doubt,suppose i have 2 jsp,1 servlet,1 web.xml file.i need to redirect 1 page via servlet with help of web.xml file.Here just i need to redirect to mainCtrl.java
ModuleNotFoundError: No module named 'async-request'
ModuleNotFoundError: No module named 'async-request'  Hi, My... 'async-request' How to remove the ModuleNotFoundError: No module named 'async-request' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'async-request'
ModuleNotFoundError: No module named 'async-request'  Hi, My... 'async-request' How to remove the ModuleNotFoundError: No module named 'async-request' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'async-request'
ModuleNotFoundError: No module named 'async-request'  Hi, My... 'async-request' How to remove the ModuleNotFoundError: No module named 'async-request' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'async-request'
ModuleNotFoundError: No module named 'async-request'  Hi, My... 'async-request' How to remove the ModuleNotFoundError: No module named 'async-request' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'async-request'
ModuleNotFoundError: No module named 'async-request'  Hi, My... 'async-request' How to remove the ModuleNotFoundError: No module named 'async-request' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'async-request'
ModuleNotFoundError: No module named 'async-request'  Hi, My... 'async-request' How to remove the ModuleNotFoundError: No module named 'async-request' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'django-request'
ModuleNotFoundError: No module named 'django-request'  Hi, My... named 'django-request' How to remove the ModuleNotFoundError: No module named 'django-request' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'oai-request'
ModuleNotFoundError: No module named 'oai-request'  Hi, My Python... 'oai-request' How to remove the ModuleNotFoundError: No module named 'oai-request' error? Thanks   Hi, In your python environment
ModuleNotFoundError: No module named 'pre-request'
ModuleNotFoundError: No module named 'pre-request'  Hi, My Python... 'pre-request' How to remove the ModuleNotFoundError: No module named 'pre-request' error? Thanks   Hi, In your python environment
ModuleNotFoundError: No module named 'production-request'
ModuleNotFoundError: No module named 'production-request'  Hi, My... named 'production-request' How to remove the ModuleNotFoundError: No module named 'production-request' error? Thanks   Hi, In your
ModuleNotFoundError: No module named 'request_factory'
ModuleNotFoundError: No module named 'request_factory'  Hi, My... named 'request_factory' How to remove the ModuleNotFoundError: No module named 'request_factory' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'request-id'
ModuleNotFoundError: No module named 'request-id'  Hi, My Python... 'request-id' How to remove the ModuleNotFoundError: No module named 'request-id' error? Thanks   Hi, In your python environment you
ModuleNotFoundError: No module named 'request-lifecycle'
ModuleNotFoundError: No module named 'request-lifecycle'  Hi, My... named 'request-lifecycle' How to remove the ModuleNotFoundError: No module named 'request-lifecycle' error? Thanks   Hi, In your
ModuleNotFoundError: No module named 'request_limiter'
ModuleNotFoundError: No module named 'request_limiter'  Hi, My... named 'request_limiter' How to remove the ModuleNotFoundError: No module named 'request_limiter' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'request-network'
ModuleNotFoundError: No module named 'request-network'  Hi, My... named 'request-network' How to remove the ModuleNotFoundError: No module named 'request-network' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'request-session'
ModuleNotFoundError: No module named 'request-session'  Hi, My... named 'request-session' How to remove the ModuleNotFoundError: No module named 'request-session' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'request-session'
ModuleNotFoundError: No module named 'request-session'  Hi, My... named 'request-session' How to remove the ModuleNotFoundError: No module named 'request-session' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'request-token'
ModuleNotFoundError: No module named 'request-token'  Hi, My... 'request-token' How to remove the ModuleNotFoundError: No module named 'request-token' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'request-validator'
ModuleNotFoundError: No module named 'request-validator'  Hi, My... named 'request-validator' How to remove the ModuleNotFoundError: No module named 'request-validator' error? Thanks   Hi, In your
ModuleNotFoundError: No module named 'request-verification'
ModuleNotFoundError: No module named 'request-verification'  Hi...: No module named 'request-verification' How to remove the ModuleNotFoundError: No module named 'request-verification' error? Thanks   Hi
ModuleNotFoundError: No module named 'aiohttp-request'
ModuleNotFoundError: No module named 'aiohttp-request'  Hi, My... named 'aiohttp-request' How to remove the ModuleNotFoundError: No module named 'aiohttp-request' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'async-request'
ModuleNotFoundError: No module named 'async-request'  Hi, My... 'async-request' How to remove the ModuleNotFoundError: No module named 'async-request' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'backports.method_request'
ModuleNotFoundError: No module named 'backports.method_request'  Hi...: No module named 'backports.method_request' How to remove the ModuleNotFoundError: No module named 'backports.method_request' error? Thanks  
ModuleNotFoundError: No module named 'bottle-request'
ModuleNotFoundError: No module named 'bottle-request'  Hi, My... named 'bottle-request' How to remove the ModuleNotFoundError: No module named 'bottle-request' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'django-request'
ModuleNotFoundError: No module named 'django-request'  Hi, My... named 'django-request' How to remove the ModuleNotFoundError: No module named 'django-request' error? Thanks   Hi, In your python

Ads