Home Answers Viewqa JSP-Servlet Getting file size

 
 


Jagadeesh
Getting file size
2 Answer(s)      4 years and 8 months ago
Posted in : JSP-Servlet

View Answers

November 11, 2008 at 12:31 AM


Hi friend,


<%@ page language="java" %>

<HTml>

<HEAD>
<TITLE>Display file upload form to the user</TITLE>

</HEAD>

<% // for uploading the file we used Encrypt type of multipart/form-data and input of file type to browse and submit the file %>

<BODY>
<FORM ENCTYPE="multipart/form-data" ACTION="SingleFileUpload_Action.jsp" METHOD=POST>

<br><br><br>

<center>
<table border="2" bgcolor=#ccFDD00 cellspacing="0" cellpadding="0">

<tr>
<center>
<td colspan="2">
<p align="center"><B>Single file Uploading in JSP</B>
<center>
</td></tr>
<tr><td><b>Choose the file To Upload:</b></td>

<td><INPUT NAME="F1" TYPE="file" size="40"></td></tr>

<tr>
<td colspan="2">
<p align="center">
<INPUT TYPE="submit" VALUE="Send File" ></p></td></tr>

</table>

</center>

</FORM>

</BODY>

</HTML>

-------------------------------------------

November 11, 2008 at 12:34 AM


file action code.

<%@ page import="java.io.*" %>

<%

//to get the content type information from JSP Request Header

String contentType = request.getContentType();

//here we are checking the content type is not equal to Null and as well as the passed data from mulitpart/form-data is greater than or equal to 0

if ((contentType != null) && (contentType.indexOf("multipart/form-data") >= 0)) {

DataInputStream in = new DataInputStream(request.getInputStream());

//we are taking the length of Content type data

int formDataLength = request.getContentLength();

byte dataBytes[] = new byte[formDataLength];

int byteRead = 0;

int totalBytesRead = 0;

//this loop converting the uploaded file into byte code

while(totalBytesRead < formDataLength) {

byteRead = in.read(dataBytes, totalBytesRead, formDataLength);

totalBytesRead += byteRead;

}



String file = new String(dataBytes);

//for saving the file name

String 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;

//extracting the index of file

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;



// creating a new file with the same name and writing the content in new file

FileOutputStream fileOut = new FileOutputStream(saveFile);

fileOut.write(dataBytes, startPos, (endPos - startPos));

fileOut.flush();

fileOut.close();



%><Br><table border="2"><tr><td><b>You have successfully upload the file by the name of:</b>

<% out.println(saveFile); %></td></tr></table> <%

}

%>


-------------------------------------------------

Visit for more information.

http://www.roseindia.net/jsp/file_upload/index.shtml

Thanks.









Related Pages:
Getting file size - JSP-Servlet
Getting file size  Hello friends, I am doing a jsp project. File uploading is one part of that. When uploading i have to check the file size. For this i used the java method .length(). But, for all type of files
Getting the Size of a File in Java
Java file get size In this section, you will learn how to get the size... then calls the method length() which returns the size of the file in bytes. Then we have converted the size of file from bytes to kilo bytes. Here is the code
Getting ResultSet size - JSP-Servlet
Getting ResultSet size  Hello friends, what..., The "fetch size" is a hint to the driver to control internal buffering within... is also free to ignore the hint. A "fetch size" of 0 means "transfer all
track file size changes
track file size changes  How to track a change in file size
file size - JSP-Servlet
the file size. Anyone help me how to check the file size with this coding. I tried...file size  Hello friends, I got the file upload coding in the roseindia at the following url: http://www.roseindia.net/jsp/file
Getting Parameter from a css styled jsp file to a java servlet file...
Getting Parameter from a css styled jsp file to a java servlet file... ... is it not getting the parameter??? How do i accept the value in the servlet file...; <h1 class="art-logo-name"><font size="10" face="Palatino
Java file get size
Java file get size In this section, you will learn how to get the size... then calls the method length() which returns the size of the file in bytes. Then we have converted the size of file from bytes to kilo bytes. Here is the code
Image Size
; This program helps you in setting an image and getting the size of its... Image Size       This Java awt (Abstract Windowing Toolkit) tutorial describes about the image size
limiting file size - JSP-Servlet
limiting file size  I 'am unable to limit the size of file usin jspx.Can some body guide me ? Thanks
limiting file size - JSP-Servlet
limiting file size  I 'am unable to limit the size of file using... uploadMaxFileSize 100m Set the size limit... 100k Set the threshold size - files
File size limit - JSP-Servlet
File size limit  i am still facing same issue. After adding parameter in web.xml web application is giving error.here is my web.xml file... /WEB-INF/tlds/c.tld   Hi friend, To set the file
Problem in getting file path in js
Problem in getting file path in js  hi, I am using ofbiz framework , How i can get the file path in javascript for input type = "file" and i am using only firefox not other browser. so please help me it is very important for me
How a split a file into many based on size?
How a split a file into many based on size?  Consider a text file which has a size of 400 bytes, i need to split up the contents in that 400 bytes file into 100 bytes of separate files.So there should be totally 4 files should
Difference in size of .java & its respective .class file
Difference in size of .java & its respective .class file  Hai, I had created & compiled a java class named sample.java. The size of sample.java is 340KB. Whereas the size of its respective class file sample.class is 137KB
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
Get File Size
Get File Size      ... in understanding  Get File Size. For this we have a class name" Get File... display the list of array file using for loop. Now in order to evaluate the size
How to adjust a size of a pdf file
How to adjust a size of a pdf file   ... adjust the size of a pdf file irrespective of the fact whether it exists or not. If it exists then its fine, otherwise a pdf file will be created. 
getting dropdown values using apache commons in servlet
); } i tried to debug and i am getting the wright file path but while proceeding...getting dropdown values using apache commons in servlet  i want to get dropdown values in my program in order to merge the string to my file path so
Limiting the Size of a Log File in Java
Limiting the Size of a Log File in Java  ... to limit the size of a log file in Java. Log files haven't any boundation at the file creation time but Java provides the facility for limiting the size of a log
Getting the Maximum Size of a Preference Key and Value
Getting the Maximum Size of a Preference Key and Value       This section demonstrates you to get the maximum size of Preference key and value. You can see in the given example that we have
Jmagick get image size
size when using Jmagick ImageInfo ii = new ImageInfo(path+"\"+file...Jmagick get image size  Hi, How I can get the image size while using... to get the image size when using Jmagick ImageInfo ii = new ImageInfo(path
Getting path name of uploading file in struts 2.1.8
Getting path name of uploading file in struts 2.1.8  In my question I used struts 2.8.1 instead of 2.1.8. Sorry
Getting path name of uploading file in struts 2.8.1
Getting path name of uploading file in struts 2.8.1  Hai! I am using struts 2.8.1. I want to upload file into database with the path name. How can I can get the original path name of the file in struts 2.8.1? It only display
Getting File path error - JSP-Servlet
Getting File path error  I have a requirement where i need to get certain properties on application login page start itself. I an currently using String str = request.getRealPath("/")+"a" + System.getProperty( "file.separator
what is the default buffer size for bufferedreader
text file efficiently? Thanks   Hi, The default buffer size...what is the default buffer size for bufferedreader  Hi, I am writing a program in Java for reading the big text file. I want to know what
pdf default size
pdf default size      ... the default size of pdf. Its options are from A0 to A10. These text explains the ISO 216 paper size system and the ideas behind its design. It will adjust the size
Getting an exception - JSP-Servlet
Getting an exception  thanks sir for u r reply ,but i already added that jar file sir even though i m getting an exception sir please help me sir ... thanks in advance..  Hi Friend, You can do one thing, replace
Getting an exception - JSP-Servlet
Getting an exception   sir i am Getting following exception when... in advance...   Hi Friend, Have you added poi-3.6-20091214.jar file in your eclipse? It seems that this jar file is not added. Thanks
Getting an exception - JSP-Servlet
Getting an exception  sir i changed to that ie InputStream myInput1...(myInput1); but when i click on a browse button and attached a file and then click on a read button to read a file it is displaying a new file instead
Getting image pixel values
Getting image pixel values  how to get image pixels values on mouse... { File file = new File("c:/rose1.jpg"); BufferedImage image = javax.imageio.ImageIO.read(file); GetPixels test = new GetPixels(image
Getting an exception - JSP-Servlet
Getting an exception  sir i m Getting an following exception while... MimeBodyPart(); ////// attach the file to the message...]); /////// adding the file to mimebosypart mp.addBodyPart(mbp2
Im not getting validations - Struts
Im not getting validations  I created one struts aplication im using DynaValidations I configured validation.xml and validation-rules.xml also in s-c.xml But Im unable to get validations This is my validation.xml file
Getting error java.lang.ClassNotFoundException: com.mysql.jdbc.Driver.
Getting error java.lang.ClassNotFoundException: com.mysql.jdbc.Driver.  I am doing project in JSP with database WampServer and glassfish as server...;   Hi Friend, Do you have mysql-connector-java-5.0.5.jar file inside
Java MappedByteBuffer example, How to create a large size file in java.
Java MappedByteBuffer example, How to create a large file in java. In this tutorial, you will see how to create a large file with the help...;} } Output C:\>java CreateFile File
Java get Folder Size
Java get Folder Size       In this section, you will learn how to get the size of specified folder.  In the given example, we are going to show you the size
How to create a zip file and how to compare zip size with normal text file
How to create a zip file and how to compare zip size with normal text file .../ZipCreateExample.shtml but when i verified the size of zip and normal file,normal file is showing... file based on one text file.i saw the code in the below link which was provided
How to decrease image thumbnail size in java?
How to decrease image thumbnail size in java?  Hi, I my web application I have to create the thumbnail size image from the image file uploaded by user. How to decrease image thumbnail size in java? Thanks
Get all file size on FTP Server
In this section we are going to describe how to get size of FTP server files using java
getting a problem in execution - Development process
the whole file i am getting a problem like in server.properties file...getting a problem in execution  hi friends i have a problem in imcms... to the installation procedure all the procedure is completed but it is getting
Getting ..Photograph.jpg not found - Swing AWT
Getting ..Photograph.jpg not found  hello sir thankyou very much for the code ... but sir here only other values are getting displayed...but not image for image it is comming Couldn't find file: Photograph.jpg .. why so?..I
size of the image - Java Server Faces Questions
size of the image  Hello, how to set the size of the image while Inserting image in the pdf file using i text  Hi friend,import java.io....("Pdf file successfully create."); doc.close
What is the maximum size of a file that can be uploaded using PHP and how can we change this?
What is the maximum size of a file that can be uploaded using PHP and how can we change this?  What is the maximum size of a file that can be uploaded using PHP and how can we change
how to set fetch size for jdbc odbc driver
how to set fetch size for jdbc odbc driver  What is the default fetch size for the JDBC ODBC driver and how can i fetch a set of results for JDBC... size of JDBC drivers is 10. In general default fetch size of JDBC drivers is 10
getting an error when set classpath
getting an error when set classpath  Hello Everyone! i have used hssf model for reading excel sheet in my project & i have set classpath for that jar file but when i compile the program it will compile correctly but when i
Getting a absolute path
Getting a absolute path       To find a file or directory it is very necessary to know the path of the file or directory so that you can access it. If you know the path
Struts2 Filter not getting initialised in WAS 8.0
Struts2 Filter not getting initialised in WAS 8.0  Hi All, Am facing... in Tomcat 5.5 and deployed in WAS 8.0 When its being launched, am getting the error...-pattern>/*</url-pattern> </filter-mapping> <welcome-file
Use of size and getInputStream method of ZipFile.
size() returns the number of entry in a associated zip file...Use of size and getInputStream method of ZipFile. In this tutorial, We will discuss the use of size and getInputStream method. These methods 
Controlling Java Heap Size Memory Allocation
Controlling Java Heap Size Memory Allocation  Hi, Tell me about Controlling Java Heap Size Memory Allocation? I have to run java program from console and allocate 2GB of RAM to Java Heap Size. Thanks   Hi, You can
JSP Getting Started, Getting Started With JSP
Getting Started with JSP This page is all about getting started with JSP... tutorials, but it's not easy to find Getting stated tutorial. Here in the page we... to run the JSP file. Read how to download install and use Tomcat at http
File IO
Getting the Size of a File Count lines of a particular file...; Getting an absolute path and Parents of a Filename File IO  ... are packaged into java.io. package. File Management with the help