24 bytes

  • HOME
  • Algorithms
    blue
  • Bubble Sort
  • Bucket Sort
  • Insertion Sort
  • Selection Sort
  • Merge Sort
  • Quick Sort
  • Heap Sort
  • Counting Sort
  • Stack
  • Queue
  • Double Linked List
  • Binary Search Tree
  • Towers OF Hanoi

  • Java Programs
    green bullet
  • Download RMI Calculator
  • Download file
  • Clock applet
  • File Upload
  • JDOM Parser
  • Client server
  • Udp Client server
  • Sudoku Solver

  • System Programming
  • Newtwork Sniffer
  • Good Links
    green bullet
  • Free Source Code
  • Top Coder
  • Code Project
  • Learn Today
  • Concept
  • Intresting Programs
    green bullet
  • Prime Number
  • GCD Euclids
  • Permutations


  • Google
     
    Web 24bytes.com



    Simple FIle Upload Using JSP
    This program only allows upload of html files which can be customised to upload files of any type by changing the if (contentType.equals("text/html")) appropriately
    The uploaded file is saved in C:\ drive as demo.html
    
    //upload.jsp a form to select the file to be uploaded
    <form action="uploader.jsp" enctype="multipart/form-data" method="POST">
    <input type="file" name="myFile">
    <input title="Upload selected file to the current working directory" type="Submit" class="button" name="Submit" value="Upload">
    </form>
    //uploader.jsp
    <% @ page import="java.util.*,java.text.*,java.io.*, java.io.IOException, java.rmi.RemoteException, javax.servlet.http.HttpServletRequest "%>
    <%
    class uploadThis
    {
    public String upLoadFile(HttpServletRequest request) throws IOException, RemoteException, Exception {
    int lineSize = 128;
    String userAgent="";
    String fileName="";
    String newLine=new String();
    String contentType="";
    FileOutputStream fs2=null;
    PrintStream fs=null;
    try {
    ServletInputStream in = request.getInputStream();
    byte[] line = new byte[256];
    int inDex = 0;
    int i = in.readLine(line, 0, 256);
    int boundaryLength=i-2;
    String boundary = new String(line, 0,i-2);
    while (i != -1)
    {
    if (newLine.startsWith("Content-Disposition: form-data; name=\"")) //begining of the file
    {
    if (newLine.indexOf("filename=\"") != -1)
    {
    //check if the name of file is there in the stream
    fileName = newLine.substring(newLine.trim().lastIndexOf("\"", newLine.trim().length() - 2), newLine.length()).replaceAll("\"", "");
    userAgent+= boundary+"hey got the file name "+fileName+"
    "+"c:/"+fileName; fs2 = new FileOutputStream("c:/demo.html"); fs = new PrintStream(fs2); } } if (newLine.startsWith("Content-Type")) //get the content type from the stream { newLine = newLine.replaceAll("Content-Type: ", ""); userAgent+="got the content Type "+newLine.trim()+"
    "; contentType=newLine.trim(); i = in.readLine(line, 0, 256); newLine = new String(line, 0, i); } if (contentType.equals("text/html")) { if ((new String(line, 0, i).startsWith(boundary))) { //End of File this boundary is a staret and end of a document uploaded its more or less like this -----------------------------265001916915724 (some random number) fs.close(); } else { if (!newLine.trim().equals("")) { fs.print(newLine.substring(0,newLine.length()).trim()+""); } } } i = in.readLine(line, 0, 256); newLine = new String(line, 0, i); } } catch(Exception e){} return userAgent; } } %> <% out.write (System.getProperty("java.io.tmpdir")+"
    "); uploadThis up = new uploadThis(); out.write (up.upLoadFile(request)); %> Simple FIle Upload Using JSP
    request a code or suggestion


    Email:
    Category


    Google
     
    Web 24bytes.com
    . original template by Aran Down.