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 .

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 Tutorials/Questions & Answers:
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
Advertisements
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
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
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
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
files
/core/files/storeobjectsinFile.html
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:ADS_TO_REPLACE_1 import java.io.*; import
HTML codes
HTML codes  Hi, I am trying to find HTML codes to learn HTML. Can any one html me? Thanks (adsbygoogle = window.adsbygoogle || []).push({});   Hi, Please check HTML examples and HTML5 Tutorials page
codes for banking databases
codes for banking databases  sir plz give me codes for creating a banking database using java
codes for banking databases
codes for banking databases  sir PLZ give me codes for banking database using java
ModuleNotFoundError: No module named 'codes'
ModuleNotFoundError: No module named 'codes'  Hi, My Python program is throwing following error: ModuleNotFoundError: No module named 'codes' How to remove the ModuleNotFoundError: No module named 'codes'
ModuleNotFoundError: No module named 'codes'
ModuleNotFoundError: No module named 'codes'  Hi, My Python program is throwing following error: ModuleNotFoundError: No module named 'codes' How to remove the ModuleNotFoundError: No module named 'codes'
ModuleNotFoundError: No module named 'codes'
ModuleNotFoundError: No module named 'codes'  Hi, My Python program is throwing following error: ModuleNotFoundError: No module named 'codes' How to remove the ModuleNotFoundError: No module named 'codes'
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
ANSI Color Codes with Python
ANSI Color Codes with Python   Use ANSI codes to play with colors and styles. Create a program that will spit out a user inputted string in a color, style, or background color that they can choose. To do this, you'll need
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
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 and installing jQuery UI       Downloading and installing jQuery UI There are Three ways of downloading or adding  jQuery UI : First :ADS
convertion before downloading
convertion before downloading  convert zip file which is in server to doc while before downloading
ModuleNotFoundError: No module named 'coupon-codes'
ModuleNotFoundError: No module named 'coupon-codes'  Hi, My Python... 'coupon-codes' How to remove the ModuleNotFoundError: No module named 'coupon-codes' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'exit-codes'
ModuleNotFoundError: No module named 'exit-codes'  Hi, My Python... 'exit-codes' How to remove the ModuleNotFoundError: No module named 'exit-codes' error? Thanks   Hi, In your python environment you
ModuleNotFoundError: No module named 'iata_codes'
ModuleNotFoundError: No module named 'iata_codes'  Hi, My Python... 'iata_codes' How to remove the ModuleNotFoundError: No module named 'iata_codes' error? Thanks   Hi, In your python environment you
ModuleNotFoundError: No module named 'mobile-codes'
ModuleNotFoundError: No module named 'mobile-codes'  Hi, My Python... 'mobile-codes' How to remove the ModuleNotFoundError: No module named 'mobile-codes' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'prefix-codes'
ModuleNotFoundError: No module named 'prefix-codes'  Hi, My Python... 'prefix-codes' How to remove the ModuleNotFoundError: No module named 'prefix-codes' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'rkm-codes'
ModuleNotFoundError: No module named 'rkm-codes'  Hi, My Python... 'rkm-codes' How to remove the ModuleNotFoundError: No module named 'rkm-codes' error? Thanks   Hi, In your python environment you
ModuleNotFoundError: No module named 'coupon-codes'
ModuleNotFoundError: No module named 'coupon-codes'  Hi, My Python... 'coupon-codes' How to remove the ModuleNotFoundError: No module named 'coupon-codes' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'exit-codes'
ModuleNotFoundError: No module named 'exit-codes'  Hi, My Python... 'exit-codes' How to remove the ModuleNotFoundError: No module named 'exit-codes' error? Thanks   Hi, In your python environment you
ModuleNotFoundError: No module named 'funny-codes'
ModuleNotFoundError: No module named 'funny-codes'  Hi, My Python... 'funny-codes' How to remove the ModuleNotFoundError: No module named 'funny-codes' error? Thanks   Hi, In your python environment
ModuleNotFoundError: No module named 'iata_codes'
ModuleNotFoundError: No module named 'iata_codes'  Hi, My Python... 'iata_codes' How to remove the ModuleNotFoundError: No module named 'iata_codes' error? Thanks   Hi, In your python environment you
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
No complete codes for Simple Form Controlle Example
No complete codes for Simple Form Controlle Example  No complete codes
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
Java Example Codes and Tutorials in 2017
Java Example Codes and Tutorials in 2017  Hi, Which is the best tutorials to learn Java programming language in 2014? Thanks   HI, Check tutorial at Java tutorials. All Java tutorials at Java Tutorials and examples
About downloading Tomcat 7.x version
About downloading Tomcat 7.x version  Hi Kindly tell me from which website i can download Tomcat 7.x version
ModuleNotFoundError: No module named 'iso-language-codes'
ModuleNotFoundError: No module named 'iso-language-codes'  Hi, My... named 'iso-language-codes' How to remove the ModuleNotFoundError: No module named 'iso-language-codes' error? Thanks   Hi, In your
ModuleNotFoundError: No module named 'py-polar-codes'
ModuleNotFoundError: No module named 'py-polar-codes'  Hi, My... named 'py-polar-codes' How to remove the ModuleNotFoundError: No module named 'py-polar-codes' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'boois_return_codes'
ModuleNotFoundError: No module named 'boois_return_codes'  Hi, My... named 'boois_return_codes' How to remove the ModuleNotFoundError: No module named 'boois_return_codes' error? Thanks   Hi, In your
ModuleNotFoundError: No module named 'cloudfront-edge-codes'
ModuleNotFoundError: No module named 'cloudfront-edge-codes'  Hi...: No module named 'cloudfront-edge-codes' How to remove the ModuleNotFoundError: No module named 'cloudfront-edge-codes' error? Thanks   Hi
ModuleNotFoundError: No module named 'django-countries-with-calling-codes'
ModuleNotFoundError: No module named 'django-countries-with-calling-codes' ...: ModuleNotFoundError: No module named 'django-countries-with-calling-codes' How to remove...-codes' error? Thanks   Hi, In your python environment you have
ModuleNotFoundError: No module named 'django-script-codes'
ModuleNotFoundError: No module named 'django-script-codes'  Hi, My... named 'django-script-codes' How to remove the ModuleNotFoundError: No module named 'django-script-codes' error? Thanks   Hi
ModuleNotFoundError: No module named 'iso-language-codes'
ModuleNotFoundError: No module named 'iso-language-codes'  Hi, My... named 'iso-language-codes' How to remove the ModuleNotFoundError: No module named 'iso-language-codes' error? Thanks   Hi, In your
HTML color codes
HTML Color Codes In this tutorial we will show you how to get the different color codes for designing your HTML pages. You will be able to find the  hexadecimal color code by color name. We have also provided the tool to create
Files
files

Ads