files uploding and downloading codes

Ads
 

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 :)

Ads









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
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
files
/core/files/storeobjectsinFile.html
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
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
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
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
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
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
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
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 '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 '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
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
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

Ads