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



Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


Jagadeesh
file size
2 Answer(s)      6 years ago
Posted in : JSP-Servlet



Advertisement
View Answers

November 14, 2008 at 1:40 AM


Hi friend,


I am sending you code. I hope that, this code will help you.


<%@ page contentType="text/html; charset=iso-8859-1" language="java" import="java.sql.*" errorPage="" %>
<%@ page import="java.util.List"%>
<%@ page import="java.util.Iterator"%>
<%@ page import="java.io.File"%>
<%@ page import="org.apache.commons.fileupload.*"%>
<%@ page import="org.apache.commons.fileupload.disk.*"%>
<%@ page import="org.apache.commons.fileupload.servlet.*"%>

<html>
<head>
<title>file upload example</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<%!
//method to return file extension
String getFileExt(String xPath){

//Find extension

int dotindex = 0;
dotindex = xPath.lastIndexOf('.');

if (dotindex == -1){ // no extension
return "";
}

int slashindex = 0; //seperator character position
slashindex = Math.max(xPath.lastIndexOf('/'),xPath.lastIndexOf('\\'));

if (slashindex == -1){ // no seperator characters in string
return xPath.substring(dotindex);
}

if (dotindex < slashindex){ //check last "." character is not before last seperator
return "";
}
return xPath.substring(dotindex);
}
%>
<%
// Check that we have a file upload request
boolean isMultipart = FileUpload.isMultipartContent(request);
out.println("isMultipart="+isMultipart+"<br>");

//Create variables for path, filename and extension
String newFilePath = application.getRealPath("/") + "LOCATIONOFFILE";
String newFileName ="";
String FileExt = "";

// Create a factory for disk-based file items
FileItemFactory factory = new DiskFileItemFactory();

// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload(factory);

// Parse the request
List /* FileItem */ items = upload.parseRequest(request);

// Process the uploaded items
Iterator iter = items.iterator();
//Form fields


November 14, 2008 at 1:41 AM


while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();

if (item.isFormField()) {
String name = item.getFieldName();
String value = item.getString();
if (name.equals("newFileName")) {
newFileName = value;
}
out.println("Form Field Name= " + name + "<br>");
out.println("Form Field Value= " + value + "<br><br>");
}
else {
String fieldName = item.getFieldName();
String fileName = item.getName();
FileExt = getFileExt(fileName);
String contentType = item.getContentType();
boolean isInMemory = item.isInMemory();
long sizeInBytes = item.getSize();
out.println("fieldName= " + fieldName + "<br>");
out.println("fileName= " + fileName + "<br>");
out.println("FileExt= " + FileExt + "<br>");
out.println("contentType= " + contentType + "<br>");
out.println("isInMemory= " + isInMemory + "<br>");
out.println("sizeInBytes= " + sizeInBytes + "<br>");
if (fileName.equals("") || sizeInBytes==0){
out.println("Not a valid file.<br>No upload attempted.<br><br>");
} else {
File uploadedFile = new File(newFilePath+"/", newFileName+FileExt);
try{
item.write(uploadedFile);
out.println(fileName+" was successfully uploaded as "+ newFileName+FileExt +".<br><br>");
}
catch (java.lang.Exception e) {
out.println("Errors prevented the file upload.<br>"+fileName+ " was not uploaded.<br><br>");
}
}
}
}
%>
</body>
</html>
---------------------------------------

Visit for more information.

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

Thanks.


Related Tutorials/Questions & Answers:
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
Advertisements
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
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
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
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
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
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. 
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
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
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
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
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
Size of commarea
Size of commarea  hii, What is the size of commarea?   hello, Default size of commarea is 65k
Size in FLex
Size in FLex  Hi..... What is the difference between width, explicitWidth, measuredMinWidth, measuredWidth, and percentWidth? please tell me about that difference Thanks
Size in FLex
Size in FLex  Hi... I just want to know about... What happens in measure()? measuredHeight, measuredWidth, measuredMinHeight, measuredMinWidth are set. please give me an example with description... Thanks
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
Servletoutputstream size limit.
Servletoutputstream size limit.  What is the maximum size of ServletOutputStream
javascript max size
javascript max size   How to retrive Max size in JavaScript
Servletoutputstream size limit.
Servletoutputstream size limit.  What is the maximum size of ServletOutputStream?   By default size is set to 10MB.You can increase your message size maximum 2000000000 bytes. That is size limit is 2000000000
hibernate.jdbc.batch_size
In this section, you will learn about the hibernate.jdbc.batch_size of the hibernate.cfg.xml.
Fixed size tables
Fixed size tables  Can anyone help me in creating a fixed size table in mysql database
calculate size of array
calculate size of array  Is it possible to calculate the size of array using pointers in Java
size of all primitive data
size of all primitive data   java program for to find the size of all primitive data types in java
setting web page size
setting web page size  How to set the webpage size in Java
Java Class Size
Java Class Size  How to determine size of java class
 

 

 

DMCA.com