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



Home Answers Viewqa JSP-Servlet files uploding and downloading codes

Have Programming Question? Ask it here!
 
 
 


rasanga ratnayake
files uploding and downloading codes
3 Answer(s)      3 years and a month 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
HTML Color Codes
:5px 0px 5px 5px; } HTML Web color codes In this page we have created a list of most used HTML Web color codes. We have shown the color... color codes: Name/String Code Name Code
downloading created xml file - Java Beginners
downloading created xml file  Hi... I've an application where i need to create an xml file and when a button is clicked it has to prompt me for saving the xml file so i created xml file successfully and whats the problem
header files
header files   do you have a all header files of java
what is wrong with my JSP codes for updating a form?
what is wrong with my JSP codes for updating a form?  Hi experts, I have tried to emulate a code from a textbook but it doesn't work in my program.... Name - String. Here's snippet of my codes:- <% Connection conn = null
Uploading files
Uploading files  Hi, Please provide html code for my question. I need to insert the browsed files temporarily to text area while attaching more than one files during mailing
validation files
validation files  If I place validation files in the folder where the action classes are present,will there any error arise
Downloading & Installing Wamp server
Downloading & Installing WAMP server In this section we will quickly show you the process of downloading and installing WAMP server. The use of WAMP... to the browser's address bar). After downloading the .exe file,install the file
Downloading and Installing WebLogic server 6.0
Downloading and Installing WebLogic server 6.0          More J2EE
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 and installing JUnit 4.3.1 s
Downloading and installing JUnit 4.3.1 s       In this section, downloading JUnit 4.3.1 address... program whether it is functioning well or not. Downloading : You can download
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
Advertisements
 

 

 

DMCA.com