rasanga ratnayake
files uploding and downloading codes
3 Answer(s)      4 years and 2 months ago
Posted in : JSP-Servlet

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

Advertisement
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
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
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
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
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
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: 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 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
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
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
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
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
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
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
No complete codes for Simple Form Controlle Example
No complete codes for Simple Form Controlle Example  No complete codes
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
Downloading and installing jQuery Transform plug-in
Downloading and installing jQuery Transform plug-in       Downloading and installing jQuery Transform plug-in You can download it from the below link
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
downloading a file directly from mysql using java
downloading a file directly from mysql using java  Hi, I am trying to come up with a code to download a file that is on a mysql database (in form of a blob) without using url. Can anyone tell me how it can be done because I am
downloading excel file using Java and springs
downloading excel file using Java and springs  I need to find out how to download an excel file using the spring framework in java. Please help me out as I am new to springs and its urgent