Home Answers Viewqa JSP-Servlet files uploding and downloading codes

 
 


rasanga ratnayake
files uploding and downloading codes
3 Answer(s)      a year and 11 months ago
Posted in : JSP-Servlet

any one know JSP codes for upload files,download files and delete files from a created virtual memory .

View Answers

June 3, 2011 at 2:52 PM


1)page.jsp:

<%@ page language="java" %>
<HTML>
<FORM ENCTYPE="multipart/form-data" ACTION="uploadandstore.jsp" METHOD=POST>
<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>
</HTML>

2)uploadandstore.jsp:

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

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();
%>
    <b>You have successfully upload the file by the name of:</b>
     <%
 out.println(saveFile);
   }
%>
    <a href="viewFiles.jsp">View Files</a>

June 3, 2011 at 2:54 PM


3)viewFiles.jsp:

<%@ page import="java.io.*"%>
<html>
<table>
<tr><th>File Name</th><th>View File</th><th>Delete File</th>
<%
File f = new File("C:/UploadedFiles");
        File[] files = f.listFiles();
        for(int i=0;i<files.length;i++){
            String name=files[i].getName();
            String path=files[i].getPath();
%>
<tr><td><%=name%></td><td><a href="download.jsp?f=<%=path%>">View</a></td><td><a href="deleteFile.jsp?f=<%=path%>">Delete</a></td></tr>
     <%
        }
%>
</table>
</html>

4)deleteFile.jsp:

<%@ page import="java.io.*"%>
<%
String file=request.getParameter("f");
    File f = new File (file);
    f.delete();
    response.sendRedirect("viewFiles.jsp");

    %>

5)download.jsp:

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

<%!
public static String getMimeType(String fileUrl)
    throws java.io.IOException, MalformedURLException 
  {
    String type = null;
    URL u = new URL(fileUrl);
    URLConnection uc = null;
    uc = u.openConnection();
    type = uc.getContentType();
    return type;
  }

%>
 <%
    String file=request.getParameter("f");
    File f = new File (file);
    String filename=f.getName();
    String type=getMimeType("file:"+file);

    response.setContentType (type);
    response.setHeader ("Content-Disposition", "attachment;     filename=\""+filename+"\"");

    String name = f.getName().substring(f.getName().lastIndexOf("/") + 1,f.getName().length());
    InputStream in = new FileInputStream(f);
        ServletOutputStream outs = response.getOutputStream();

        int bit = 256;
        int i = 0;
            try {
                    while ((bit) >= 0) {
                        bit = in.read();
                        outs.write(bit);
                    }
                        } catch (IOException ioe) {
                        ioe.printStackTrace(System.out);
                    }
                        outs.flush();
                    outs.close();
                    in.close(); 

        %>

June 3, 2011 at 6:33 PM


thank u sooooooooooooooo much :)









Related Pages:
files uploding and downloading codes
files uploding and downloading codes  any one know JSP codes for upload files,download files and delete files from a created virtual memory
what are the codes for uploading and downloading files in and from database?
what are the codes for uploading and downloading files in and from database?  We are designing a student's website where they can get and post study materials. We actually need the code to upload and download files in and from
php csv file uploding into mysql database table.
php csv file uploding into mysql database table.  hai friends, i have two excel files with different field names.each files having more than 30 fields. i have one mysql database table with two files fields, when ever i am
Problem in downloading files - JSP-Servlet
Problem in downloading files  Hi, i have uploaded some files using its uploading successfully. but when downloading the same file, its giving exception as below org.apache.jasper.JasperException: getOutputStream() has
JSP Upload and Downloading files - JSP-Servlet
and downloading files in JSP, I am facing a problem.. 1) The file name is getting inserted...JSP Upload and Downloading files  Respected Sir/Madam, Very... are fixed one for all types of files what I have uploaded.. I Dont know the exact
Downloading in JSP - JSP-Servlet
Downloading in JSP  Respected Sir/Madam, I am... application while downloading the uploaded file contents.. Actually my problem is I... downloaded files at the output.. THE SIZE OF THE DOWNLOADED FILE IS NOT EQUAL
Downloading and installing jQuery UI
downloading you'll get a zip containing the following files... Downloading and installing jQuery UI       Downloading and installing jQuery UI
downloading
downloading  i doing project in php with mysql. i am using xampp.i need code for downloading file
uploding an file - JSP-Interview Questions
uploding an file  i want a code for uploading a file and storing it in clog and blog using jsp ex:uuploading file using binary input steam
Downloading and Installing Flex Builder
Downloading and Installing Flex Builder  ... downloading and installing procedure for adobe flex builder. Flex Builder is an IDE... you set in your system for saving the downloaded files. If not set
Installing JSTL,Downloading JSTL, Installing JSTL
4.x and Tomcat 5.x Containers. To use JSTL two files jstl.jar and standart.jar.... Each web application must have this file. With Tomcat 5.0 these files can..., as the tld files have to be copied in application folder at a particular location
Downloading JSTL
Downloading JSTL      ... to be followed for Tomcat 4.x and Tomcat 5.x Containers. To use JSTL two files... these files can be copied from the examples folder webapps/jsp-examples/WEB-INF
show codes of this
show codes of this  search for the number of occurrence of "and" and "the" in the following sentence by writing a codes. the student like the best teacher at the end of the lessons and others
HTML codes
HTML codes  Hi, I am trying to find HTML codes to learn HTML. Can any one html me? Thanks   Hi, Please check HTML examples and HTML5 Tutorials page. Thanks
using java files - Java Beginners
using java files  how i use files in java to store as eg. (information about movies as its name) & when user ask about some movie the movie will come with a picture associated with it plz write me a couple of line codes
Downloading, Installing and Initializing Eclipse
Downloading, Installing and Initializing Eclipse  .... Downloading Eclipse: Eclipse can be obtained from eclipse?s home page http... another folder named eclipse containing all the unzipped files and folders
Downloading and Installing Flex
'  in your C:\Program Files location and after this your all downloading task... Downloading and Installing Flex   ...:\Program Files. After completion of the extraction process a folder with name
files
/core/files/storeobjectsinFile.html
Downloading and Installing Apache Axis2
Downloading and Installing Apache Axis2     Downloading and installing Apache Axis2 In this section we will download and install... in c: or d: and copy the downloaded files (axis2-1.5.1-bin.zip, axis2-1.5.1-docs
codes for banking databases
codes for banking databases  sir PLZ give me codes for banking database using java
codes for banking databases
codes for banking databases  sir plz give me codes for creating a banking database using java
files
files  write a java program to calculate the time taken to read a given number of files. file names should be given at command line.   Hello Friend, Try the following code: import java.io.*; import java.util.*; class
convertion before downloading
convertion before downloading  convert zip file which is in server to doc while before downloading
codes for displaying in calendar
codes for displaying in calendar  can i get jsp codes for displaying comments, when the pointer is placed over the particular date in calendar
codes for banking databases
codes for banking databases  sir plz give me codes for creating a banking database using netbeans as back end and java as front end
Downloading Struts & Hibernate
Downloading Struts & Hibernate   ... and setup the development environment. Downloading Hibernate Hibernate is free....zip. Save downloaded file into your hard disk. Downloading Ant Ant is a free
Needed jsp codes
Needed jsp codes   jsp code for employee payroll.producing a payslip of every employees monthly calculating the gross pay of a particular employee considering the income tax and producing a report which can be printed out
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
jsp and ejb codes - EJB
jsp and ejb codes  give me the codes to write small shopping cart project.I only want to acess from server.i want to show the client the search place... i all want to develop..please send me the codes in using jsp and ejb.because iI
Java Example Codes and Tutorials
Java Tutorials - Java Example Codes and Tutorials Java is great programming... that cause common programming errors. Java source code files are compiled... generates C header and source files that are used by C programs
Download dynamic link files to local directory - java code
Download dynamic link files to local directory - java code  Hi, I found a code that downloads static links but not dynamic link files.can anyone give a solution for downloading dynamic link files? Thanks in advance
Can you correct this codes?
Can you correct this codes?  Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 4 at searchcon.(searchcon.java:39) at search.initComponents(search.java:41) at search.(search.java:7) at payroll
Re:number of Files download problem in jsp - JSP-Servlet
Re:number of Files download problem in jsp  I write code for single file downloading , but I want to download number of files at same time , means I want stored number of files in one temp zip file then it download it. please
Download Struts 2.1.8, Downloading and installing
Download Struts 2.1.8, Downloading and installing Struts 2.1.8... Downloading Struts 2.1.8: Visit the url http://struts.apache.org/download.cgi... the file you will get following files/Directories: The apps directory contains
Downloading MyFaces example integrated with tomahawk
Downloading MyFaces example integrated with tomahawk...; Downloading :  If you are beginner in the field of JSF framework then really... will get 4 war files. myfaces-example-blank-1.1.6.war myfaces-example
java sorting codes - Java Beginners
java sorting codes  I want javasorting codes. please be kind enogh and send me the codes emmediately///  Hi Friend, Please visit the following link: http://www.roseindia.net/java/beginners/arrayexamples/ Here

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.