Getting the name of an uploaded file

Hi!
I'm uploading a file to a JSP with a HTML form that encodes the request as "multipart/form-data".
At present, a file input is sent to this JSP which handles the file:
<%@ page language="java" contentType="text/html" import="java.io.*" %>
<%@ page import="javax.servlet.*" %>
<%
     String contentType= request.getContentType();
     int boundaryIndex = contentType.indexOf("boundary=");
     String boundary = contentType.substring(boundaryIndex+9);
     int boundaryStrLength = boundary.length();
     ServletInputStream servIn = request.getInputStream();
     DataInputStream in = new DataInputStream(servIn);
     String line;
     while((line = in.readLine()) != null)
          getServletContext().log("Got a line: "+line);     
          if(line.trim().length() == 0) break;
     ByteArrayOutputStream byteOut= new ByteArrayOutputStream( request.getContentLength());
     byte[] buffer = new byte[4096];
     int len;
     while((len = in.read(buffer)) > 0)
          byteOut.write(buffer,0,len);
     byte[] outBytes = byteOut.toByteArray();
     File outputFile = new File(session.getAttribute("currentFolder") + "\\newFile");
     outputFile.createNewFile();
     FileOutputStream fileOut = new FileOutputStream(outputFile);
     fileOut.write(outBytes, 0, outBytes.length - boundaryStrLength - 8);
     fileOut.close();
     response.sendRedirect(response.encodeURL("manageFilesIndex.jsp"));
%>
     As you can see, the file is saved as 'newFile' in a certain folder.
How can I change my code so that the file is saved under the name that the user originally called it?
Thanks!
Alastair

the length() method of java.io.File class should return the file size in bytes.

Similar Messages

  • To upload the ZIP file and get the filenames available in ZIP file in ABAP

    Hi Experts,
    For my requirement, file from legacy comes as ZIP file with number of files in that.
    Please provide one code sample to upload the ZIP file from local workstation and get the filenames available in ZIP file to check few filename validation checks for the available files in report program.
    Thanks in Advance,
    Regards,
    Basani

    1. Copy the ZIP file into App server
    2. Call function
      call function 'RFC_REMOTE_PIPE'
        destination 'SERVER_EXEC'
        exporting
          command = command  " Unzip command gunzip /path & file
          read = 'X'
        tables
          pipedata = std_lines
    then you can read the files and can validate the file names

  • Need of a Webservice to get the name of the file in a Folder

    Hi All,
    I am in need of a Web service which should get the name of the file in a folder.
    Note :  Folder is created in the MOSS server.
    Thanks.

    Just to add, Mac in intergrated into AD and all I need is help in creating the script
    Thanks

  • Can we get the name of fmx file at runtime

    Hi all,
    can we get the name of fmx file which is doing some DML operation on a specific table Can we back track which fmx module has done DML on a specific table. The reason I need this because I've 7 different fmx files which are doing some DML operation on a specific table and these fmx files were used by different users. Not necessarily 1:1 For eg. one user can do DML through 3 different fmx files. So in such case if I want to want to know from which fmx file this DML has take place .How can I achieve that. I know I can get the oracle username through v$session but it will not suffice my needs. I need the name of fmx file also.
    I'm using:
    Forms [32 Bit] Version 10.1.2.0.2 (Production)
    Oracle Database 10g Release 10.2.0.1.0 - Production
    PL/SQL Release 10.2.0.1.0 - Production
    CORE    10.2.0.1.0      Production
    TNS for 32-bit Windows: Version 10.2.0.1.0 - Production
    NLSRTL Version 10.2.0.1.0 - Production
    Any help?
    Warm Regards
    Percy

    Percy,
    For that you can set the Module Field in the V$session in each form by,
    DBMS_APPLICATION_INFO.SET_MODULE(MODULE_NAME => :SYSTEM.CURRENT_FORM, ACTION_NAME => USER);Write this in each form, and then you will get the form name in the V$SESSION's module field with form name.
    Hope this helps.
    Regards,
    Manu.

  • How to get the name of the file parent

    Hi,
    i have a file in this location
    File file = new File(MyFile.doc)
    file.getParent(); returns
    C:\Documents and Settings\Administrator\xyz
    now from the string obtained i just want to get the value "xyz".i.e. i want to get the name of the parent directory.
    how is it possible

    You may use String.indexOf()if(file.getParent().indexOf("/myProject/myPackage") != -1) {
        ...Or you might use regular expressions and Matcher class (or String.matches()) in case of higher complexity.

  • When I try to convert my .pdf to Word, I get a sidebar that has fields for: the name of my pdf file, the type of file I want to convert to (In my case, Word), and a button that says "Convert".  My problem is that the "convert to" field isn't active.  I ca

    When I try to convert my .pdf to Word, I get a sidebar that has fields for: the name of my pdf file, the type of file I want to convert to (In my case, Word), and a button that says "Convert".  My problem is that the "convert to" field isn't active.  I can see the words "Microsoft Word" in pale gray, but when I click on the field, nothing happens.

    You have posted in the wrong forum.  Try posting in the forum for the product you are trying to use.
    Here is a link to a page that has links to all Adobe forums... use the drop down list to see all.
    Forum links page:
    https://forums.adobe.com/welcome

  • Changing the name of a uploaded excel file

    Hi everyone,
    I am being able to manage to change the name of a excel file when I am working on a scenario with a single file like this:
    http://imageshack.us/photo/my-images/204/67859086.jpg/
    Here goes the BUT, but when I am trying to upload several files at the same time, I am not being able to rename the files.
    http://imageshack.us/photo/my-images/694/16392664.jpg/
    When I do have several file the package works like this:
    It list all the excel files and stores them into a *.LST file, after that it stores the names on a table. After that it uses a wildcard excel file to put the info to be uploaded, the on the scenario it transforms the data and uploads it into the target tables, then I supposedly change the name but it does not work.
    Then It moves the file to another folder, and erase the name from the temp table….
    I do really appreciate your help and guidance.
    Please do not hesitate to contact me if any further information is required.
    Best regards.
    Jaison

    There is an interface as step 2. I had similar issue once when I wanted to delete Excel file that was just used via Windows ODBC driver in a step before. Solution there was to create two packages, one doing the job and a wait step at the end the other one just deletes the file. Now put the two into a master package and run it from there.
    Probably the ODBC driver has the *.xls file still in some kind of stale/cached access.

  • How do I get the name of a file in Xcode in a Cocoa-Applescript application?

    I'm trying ot create an app in Xcode using applescriptObjC. I need to get the names of files, but every time I run the app and trigger the part where the script is supposed to get the name, Xcode gives me an error saying "Can not get name of "/Users/xxxx/xxxx/xxxx.xxx" I have tried everything.
    Any help is appreciated

    can you post some relevant code?

  • Get the name of the file generated by the File Adapter

    Hello,
    I am creating XML files using the File Adapter. Each file name is unique (my_file%SEQ%.xml) and I would like to know if there is a way to get the name of the generated file so that I can sent it as an attachment in an email notification. Thanks!

    you can't get the generated filename for write operation.
    But what you can do, is to define header variable of OutboundHeader_msg type, define your own filename (based on your unique creation criteria) and then pass it to invoke activity for file creation.

  • Search a string with in some file and get the name of file

    i want to search a string in some files that are in a given directory and return the name of file having searched string.I am doing this with grep command as given below
    import java.io.*;
    public class linux_java {
    public static void main(String[] args) {
    try {
    String command = "find . | xargs grep -l Resolv /data2/opt/jakarta-tomcat-4.1.24/webapps/Oa/BOG/*.txt";
    final Process process = Runtime.getRuntime().exec(command);
    OutputStream os= process.getOutputStream();
    System.out.println("out put stream= " +os);
    PrintStream stdIn = new PrintStream(new BufferedOutputStream(process.getOutputStream()), true);
    System.out.println("Return code = " + stdIn);
    } catch (Exception e) {
    e.printStackTrace();
    i dont know how to get the name of file that are return by the execution of command through process object. Please send the code for the same

    thanks for your suggestion....
    i change the code but now it is giving error as /usr/bin/find incomplete syntax....but i am giving the right syntax....
    please confirm whether the syntax is correct...
    import java.io.*;
    import java.util.*;
    public class linux_java {
    public static void main(String[] args) {
    try {
    //String command = "ls -alt";
    String command = "find /data2/opt/jakarta-tomcat-4.1.24/webapps/Oa/BOG -type f -exec grep -sl 'Aggarwal' {} \\; 2>/dev/null";
    ///grep -l System test/*java
    System.out.println(" the command is"+command);
    final Process process = Runtime.getRuntime().exec(command);
    InputStream is = process.getErrorStream();
    InputStreamReader isr = new InputStreamReader(is);
    BufferedReader br = new BufferedReader(isr);
    String line="";
    System.out.println(" the BufferedReader is"+br.readLine());
    while ((line = br.readLine()) != null) {
    System.out.println(" the files are"+line);
    } catch (Exception e) {
    e.printStackTrace();
    }

  • Can I get the name of the jnlp file used to launch my application

    Does anyone know if there is a way to get the name of the jnlp file used to launch the currently executing code?
    Essentially in the code below a way to set myAppFileName without hard coding the string. i.e. code could exist in a utility class used by many jnlp applications.
                String myAppFileName = "myApp.jnlp";
                BasicService bs = (BasicService) ServiceManager.lookup("javax.jnlp.BasicService");
                String url = bs.getCodeBase() + myAppFileName;Seems reasonable that if the jnlp API's know about the codebase that maybe it could hold the file name somewhere too? I hope, I hope ....
    Thanks
    Mike

    Ok. Bravo
    Please mark this as 'Answered' - it will help others on the forum -

  • Get filesize and filename of uploaded file

    Hello -
    I am somewhat of a newbie and am need of some assistance.  I am using ADT to upload a file and save to a database along with its filename and filesize.  The file uploads fine, but the filename and size are not being recorded in the database. Other fields that are part of the upload form save to the dB fine.
    Here's the basic code:
    // Add columns
    $ins_UserBackgroundPapers->addColumn("FileContainer", "FILE_TYPE", "FILES", "FileContainer");
    $ins_UserBackgroundPapers->addColumn("FileName", "STRING_TYPE", "POST", "FileName");
    $ins_UserBackgroundPapers->addColumn("FileSize", "STRING_TYPE", "POST", "FileSize");
    <form name="form1" method="POST" id="form1" action="<?php echo $editFormAction; ?>" enctype="multipart/form-data">
         <input type="file" name="FileContainer" id="FileContainer" size="32" />
         <input name="FileName" id="FileName"  type="hidden" value="<?php echo $HTTP_POST_FILES['FileContainer']['name']?>" />
         <input name="FileSize" id="FileSize" type="hidden" value="<?php $HTTP_POST_FILES['FileContainer']['size'];?>" />
         <input class="submit" type="submit" name="KT_Insert1" id="KT_Insert1" value=";Upload"/>
    </form>
    I've also tried using $_FILES["FileContainer"]["name/size"] with no luck.
    Any help would be much appreciated. I'm desperate!
    Thanks.

    I think the main issue is that the file name does - unlike expected - not get stored in the FileContainer column, but I don´t have any idea why this happens without seeing the complete code of the page in question.
    In regards to retrieving the file size:
    Those hidden fields you´re trying to make use of:
         <input name="FileName" id="FileName"  type="hidden" value="<?php echo $HTTP_POST_FILES['FileContainer']['name']?>" />
         <input name="FileSize" id="FileSize" type="hidden" value="<?php $HTTP_POST_FILES['FileContainer']['size'];?>" />
    ...can´t capture the additional info you want, because the very moment the form data gets submitted, the file itself has not neccesarily been uploaded yet, in particular when ADDT´s File Upload trigger has been set to AFTER -- and it´s important to consider that the $HTTP_POST_FILES[] values are assigned at the server side and not by the browser anyway.
    However, additional information such as the size of an uploaded file doesn´t need to be stored anyway, because such information is easily retrievable by using the PHP function "filesize": http://de.php.net/filesize
    Cheers,
    Günter
    P.S: $HTTP_POST_FILES[] is deprecated since PHP version 4.1.0 -- better use $_FILES instead: http://us3.php.net/manual/en/reserved.variables.files.php

  • I name each photo buy replacing the jpeg number with the name. when I burn to disk and open disk the name is not shown. how do I get the name given to show on the burn disk?.

    after I burn the files to dvd and load to view the name does not appear. How do I get the name listed to appear on the burn disk instead of the file number?

    Naming the Photo - or giving it a title - is not the same as naming  file.
    How are you burning to dvd? With iPhoto? If so then the Title will be available in any app that understands basic metadata.
    If you want the title available in the Finder - which does not understand photo metadata  - then you'll need to export the file
    File -> Export
    In the resulting dialogue you'll see a heading for 'File Name'. Choose 'Use Title' from the drop down.
    Regards
    TD

  • How to get the name of the attachment in Sender Mail Adaper?

    hi mates,
    I've scenario a where in I need to pick up a mail with an attachment and change its extension and send it to target system using file adapter.
    I'm able to pick up the mail with attachment using sender mail adapter but not able to get the name of the attachment so that I can use it in mapping or on the adapter configuration. How do I read the name of the attachment and manupulate it?
    I looked at the thread sender mail adapter - attachment name But the solution is not described. I think Stefan is talking about the SP!2 for PI 7.0. I'm on XI 3.0 what is the equivalent SP for that, SP20? Stefan, can you please throw me more light on it?
    I appreciate your valuable inputs
    thx in adv
    praveen

    Thanks Aamir.
    Earlier I replied to that thread asking for more details...but did not see the response...
    anyways, thanks for your quick help..
    but I wonder abt the PI SP12/ XI SP20 pre-requisite. We can have adapter modules on SPs lower than PI SP12/XI SP20 as well, can we not?
    thanks
    praveen

  • How to determine the mimetype of an uploaded file (GUI_UPLOAD)?

    Hi everybody.
    I use method cl_gui_frontend_services=>gui_upload to upload files and save the binary data in the database. to preview the filetype (pdf, doc, jpg, ...) later i also need to store the mimetype.
    but how can I determine the mimetype of an uploaded file if several file extensions/formats are possible?
    thanks in advance for your help,
    Stefan

    call the function SKWF_MIMETYPE_OF_FILE_GET
    for this pass the filename , you will get the mime type.
    FILENAME                        C:\TEST.PDF  "pass this
    MIMETYPE                        application/pdf "you will get this
    FILENAME                        C:\TEST.JPG
    MIMETYPE                        image/jpeg
    FILENAME                        C:\TEST.DOC
    MIMETYPE                        application/msword

Maybe you are looking for

  • Help needed in writing a small piece of ABAP Code

    Subject : look up in an ODS and update in a characteristic routine (BI 7.0)   Hi , My requirement is : I have data coming from both legacy as well as SAP Systems. The user wants both legacy as well as SAP fields data in their reports. I have a Standa

  • Doc. in FBL1N but not in F-04

    Hi All, Do you kindly let me know,why we are not able to find the some document in F-04 even though its available in FBL1N as open item. Let us put our case....  we made a down payment to a VENDOR 10000/- and posted 15 invoice  documents of this vend

  • Thread refuses to work

    Ok, this is the weirdest thing ever happened to me ever since i started working with cellphone programming. Lately, one of my thread refuses to activate. I have this thread that communicates with a thermal printer to print something right. Well, i ha

  • OSPF load balancing across multiple port channels

    I have googled/searched for this everywhere but haven't been able to find a solution. Forgive me if I leave something out but I will try to convey all relevant information. Hopefully someone can provide some insight and many thanks in advance. I have

  • Is it best practice to use a dedicated link between NX7K pair for keep-alive?

    I have been using dedicated physical 10G link between pair of NX7k for keep-alive. Is it a best practice? It seems a waste of 10G ports because keep-alives does not need that much bandwidth. I'm thinking just configure a dedicated VLAN interface in t