How to delete one existing file before uploading a file with same name?

Hello everybody.
I am uploading a file to Tomcat server. But, the problem is:
i want to delete an existing file in the server, if i upload a fresh file with same name. In other words, "First check for the file with the same name in the server. If it exists, then delete existing file in the server and upload fresh one". If such file doesnot exist, then upload the file to server.
I have given deleteonExit()
but, for the first time when user is uploading, i want to check for the file with same name in the server.
i am pasting the code here. please help:
<!-- uploading the file -->
<%
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 contextRootPath = this.getServletContext().getRealPath("/");
contextRootPath=contextRootPath.concat("uploaded");
String file = new String(dataBytes);
String saveFile = file.substring(file.indexOf("filename=\"") + 10);
saveFile = saveFile.substring(0, saveFile.indexOf("\n"));
saveFile = saveFile.substring(saveFile.lastIndexOf("\\") + 1,saveFile.indexOf("\""));
// Create a directory; all ancestor directories must exist
File outputFile = new File(contextRootPath, saveFile);
var=outputFile.getPath();
outputFile.createNewFile();
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;
FileOutputStream fileOut = new FileOutputStream(outputFile);
fileOut.write(dataBytes, startPos, (endPos - startPos));
fileOut.flush();
fileOut.close();
outputFile.deleteOnExit();
%>Please help. Thanks for taking time.
Regards,
Ashvini

Thank you MartinHilpert,
I have one more doubt,
I am uploading the file to one folder called "uploaded". Before uploading a fresh file, i want to delete all existing files in that folder. IS that possible ?? If yes, can you please tell me how to do that ??
Regards, Thanks for your time.
Ashlvini

Similar Messages

  • How to check-in multiple files with same name having different revision num

    Hi
    Can anyone please tell me, how to check-in multiple files with the same name with different revision number using RIDC API.
    For eg:
    First I will check-in a file(TestFile.txt) into a content server with revision number 1 using RIDC API in ADF application. Then after some time, will modify the same file(TestFile.txt) and check-in again. I tried to check-in same file multiple times, however first time its checking-in correctly into server showing revision as 1, while checking-in same file again, its not giving any errror message, and also its not reflecting in server. Only one file(TestFile.txt) is reflecting in server.
    How to implement this functinality using RIDC API? Any suggestions would be helpful.
    Regards
    Raj
    Edited by: 887680 on Mar 6, 2013 10:48 AM

    Hi Srinath
    Thanks for your response. Its not cloning, its like check-in file first, then check-out the file and do some editing and then again upload the same file with different revision number using RIDC. I got the solution now.
    Regards
    Raj

  • Download and overwrite existing file with same name

    Is there a way to tell Safari to erase previous versions of downloads, ie not adding a '-1' or '-n' to the file name, but overwrite an already existing file?

    I was going to say "hurrah!" but..... no.
    I does ask me for the file name, it does ask me if I want to replace, but it does not replace the file, and just saves it in dummy-1.ppt.
    Funny thing: it has the expected behaviour for pdf files, where replacement actually occurs.
    Bug report sent.

  • Can't Overwrite Existing Files with Same Name - Likely a Mail Error?

    Couldn't find this on the forum... Hope I can explain this properly.
    When I design something and send a proof I will send a PDF through mail. When the revises come in I used to be able to just overwrite the existing files on my desktop and re-send through mail. But now programs won't let me overwrite files with the same name anymore. Basically the program says "overwrite existing file?" but then say "file is in use. Can't overwrite."
    Back in Leopard this used to happen when I would have a mail draft open that used the data of the file I sent or was about to send. I would just have to save the mail message as a draft and the file was "no longer in use" so I could proceed as usual. But now it does this all the time regardless.
    Did this make sense? It's kind of hard to explain... But basically now when I want to send a revised file (and this can happen dozen of times in a day) I have to trash the existing file I want to overwrite. Or rename the file, which is confusing for me and my clients.
    Any thoughts?

    I have the same situation as you - sending out pdfs of designed pages and wanting to overwrite them continually. It used to happen on an older version of Mail, then it disappeared and now it's back with the latest version.
    It's more than annoying, it's downright driving me nuts. In the last hour I had to quit Mail seven times.
    Please send a fix for this Apple.

  • While saving multiple attachments from mail, files with same name are added and not replaced

    While saving Multiple Attachments from Mail, existing file with same name are not overwritted but new files are added in the folder.

    Bjørn Larsen a écrit:
    Hi all
    Hope to get some help with Elements Organizer.
    I have 12-15 years of digital photos that I now want to import into my newly aquirede Adobe Elements Organizer / Photoshop. Since my Nikon names the files with continous numbers from 0001 to 9999 I have multiple files with the same name although they are not alike at all. My previous software had no problems with that since I keep the photos in separate file folders based on import date. I generally import photos after each event and so name the folder with the date and some event info (e.g. 2014.12.24 - Christmas at grandparents).
    That is a common situation, I have the same limitation for files not going over 9999 on my Canons...
    Now - when I import my photos into Elements Organizer I get a lot of error messages with "same name exist .....) Hmmmmmmm
    Please sate the exact wording of the error message, I have never seen a message stating 'same name exist...' or equivalent; only messages about files already in the catalog. Files already in the catalog mean that some files have the same 'date taken' and file size in Kb.
    Any suggestions. I'm using a mac and tried to rename files based on date taken. The mac can do that but it takes ages to go into each folder and run the renaming script there.
    I also use a similar folder creation scheme (such a date naming is the default for the downloader). That way I never get a message about duplicates for the same file names.
    However - I can't be the first or only person with this problem so I figure that some workaround must be known out there. Maybe the import action can recognize date taken or - well. Thank you very much in advance if you can help me out here.
    You can alsways set the downloader to rename the imported files with a unique new name, there are many options in the 'advanced' dialog of the downloader. I don't know about Macs, but I don't thing there is a difference.

  • How to execute one .exe file with as3 in air ?

    Hi
    How to execute one .exe file with as3 in air?
    I want do this work without fscommand .
    plize help me .

    itsdhanasaraa wrote:
    But as this a web application ... By using Runtime i'm getting some probs ..
    Let me guess, you want your web application to run a program on the client and to your surprise that's not working?
    Ain't gonna happen.
    its taking more time to execute .... that's y is there any other option to execute .exe file other than Runtime.getRuntime().exec("filename");Write proper English and you may be taken more seriously.
    1) it's not "taking more time to execute", whatever that's supposed to mean.
    2) there's no other way to execute something. Not that you should every use even that way anyway
    3) whenever you start thinking of executing external programs from Java, start thinking of not using Java in the first place.

  • There is no 'Save as' under the file menu.  How do I save a file with another name?

    Numbers help refers to the 'Save As' function in the File menu.  My version (latest) does not have a 'Save As' function.  How do I save a file with another name?

    Ross Millard wrote:
    Badunit. You have an original file and an edited file. Now you duplicate the edited file and save it with a different name. Now do you have to go and delete the changed file from which the duplicate had been made? And.... is the original still unchaned... still original?
    Ross,
    For the situation you describe, Apple has provided Duplicate and Revert. Use File > Duplicate to reach this menu:
    Regards,
    Jerry

  • In Pages how do I save a file with another name

    In Pages how do I save a file with another name?

    I don't know if there is a less cumbersome way to do this (I'm still pretty new to Lion) but This is what I do - since "Save As" seems to be gone with Lion.
    I create a duplicate document - then click the red button to close the document - and a dialog box pops up and asks if you want to save the changes - I change the name in there and then save the file under the new name.
    You can also save the copy to the desktop - get info - and then change the name if the finder window.
    I copied this from the Pages help area. These help instructions were created pre-Lion I'm sure.
    Saving a Copy of a Document
    If you want to make a copy of your document—to create a backup copy or multiple versions, for example—you can save the document using a different name or location. (You can also automate saving a backup version, as Automatically Saving a Backup Version of a Document describes.)
    To save a copy of a document: 
    Choose File > Save As and specify a name and location.
    The document with the new name remains open. To work with the previous version, choose File > Open Recent and choose the previous version from the submenu.

  • Script to find files with same names with in a folder and it sub folders.

    Looking for script to find files with same names with in a folder and it sub folders.

    Are you just looking to find if any two files underneath a folder have the same name?
    If you just want to know that a file named "whatever" exists in two folders, that's not too difficult, but you probably want to know the full path names.
    Here's one attempt:
    $ perl -MFile::Find -le 'find(\&w, "."); while (($n,$p)=each %file) {if(@{$p}>1){print join(" ",@{$p})}} sub w{push @{$file{$_}},$File::Find::name;}'That will print the pathnames on the same line of any files with the same name that appear anywhere underneath your current directory.
    It's a bit long for a "one-liner", but functional.
    Darren

  • Merging Log Files with same name

    Hello,
    I have log files with same name 00000000.jdb (because created at different machines). I would like to merge data stored in these two files.
    I tried below approaches which do not work:
    1. rename one of the files -> doesn't work as file header has file name info which doesn't match with renamed value
    2. db dump/load -> dump and load works without error. (i have to dump Entity related Database and Format related Database -- from -l option) However when i try to read the file, it gives error.
    Exception in thread "main" com.sleepycat.je.EnvironmentFailureException: (JE 5.0.103) Catalog could not be refreshed, may indicate corruption, errorFormatId=52 nFormats=51, . UNEXPECTED_EXCEPTION: Unexpected internal Exception, may have side effects.
    I am using EntityStore and thus Annotated Entities.
    Does any one has any insights?
    Thanks.

    Yuvaraj,
    As you discovered, it is not possible to use .jdb files from one environment in a different environment.  The .jdb files use an internal format with relationships between files that form the Btree.
    I'm glad to hear you were able to use DbDump and DbLoad successfully.
    --mark

  • Help - loading updated files with same name

    CS4
    When updating files with same name my CS4 automatically puts an extension _000
    I think it should ask me if I want to replace the existing file.  Is there a switch or setting that I need to change?
    Thanks.
    Alan

    Please find the solution in thread, http://forums.adobe.com/thread/707897?tstart=0.
    Hope this helps you.

  • Static Files with same name. How does apex determine which files to serve?

    Hello, I'm using apex 4.2.1.00.08 and I cannot figure out how apex manages the static files and cannot find any help in the docs (other that some high level UI description).
    The application is serving some file and I cannot find which one it is in any easy way.
    I have a workspace where there are several files that have the same name, and I cannot understand how apex figures out which one to serve, and also don't understand what is value of associating a file with an application.
    There are files associated with application 0, which don't appear to show up in the "shared components", but can be seen as
    SELECT *
    FROM wwv_flow_files
    WHERE flow_id = 0;
    and can apparently only be deleted using "SQL Commands" inside apex.
    the URL called is something like
    wwv_flow_file_mgr.get_file?p_security_group_id=13498126233076320&p_fname=myfile.css
    so apparently the only parameters that matter are the workspace and the file name. The associated application is irrelevant.
    apparently files linked to flow_id 0 have precedence over all the other files ...
    Thanks for clearing up a bit of "fog" on this issue.

    VC wrote:
    Go to that application > shared components > Static files you should see the file.the problem is that there are multiple files with that name, and that sometimes the file being served is linked to application "0" and it doesn't show in the "shared components" interface ...
    I have a workspace where there are several files that have the same name, and I cannot understand how apex figures out which one to serve, and also don't understand what is value of associating a file with an application.Static files can be uploaded to apex with optionally associating with an application within that workspace.
    Files associated with an application are referenced using *#APP_IMAGES#*
    Files not associated with an application are referenced using *#WORKSPACE_IMAGES#*I referenced with #WORKSPACE_IMAGES#, but now I see that if I use #APP_IMAGES# the URL generated will also contain the application ID. This would help to discriminate between different files with the same name but linked to different applications ...
    There are files associated with application 0, which don't appear to show up in the "shared components", but can be seen asWhy are you particularly interested in application 0?Because somehow APEX puts the files that I uploaded there ... they can be seen selecting from wwv_flow_files. Do they take precedence over all other files with the same name?
    Filename is unique for the given workspace[and application]I disagree. I have multiple files with the same name ... the root of this problem ...
    so apparently the only parameters that matter are the workspace and the file name. The associated application is irrelevant.How is your static file referenced??
    But not always, try associating an static file with and application and reference it using #APP_IMAGES# instead of #WORKSPACE_IMAGES#
    See
    http://docs.oracle.com/cd/E37097_01/doc/doc.42/e35125/concept_sub.htm#BEIDCGAJ
    http://docs.oracle.com/cd/E37097_01/doc/doc.42/e35125/ui_file_manage.htm#HTMDB06011
    Thanks, but the documentation doesn't give much details ... apparently files referenced with #WORKSPACE_IMAGES# can still resolve to files linked to specific applications ... I would like to understand the actual workflow for the various cases (file references with APP_IMAGES, referenced with WORKSPACE_IMAGES, file associated with the application, with another application, with no application, with application "0" ...).
    Also, I find it somewhat misleading that you can have files associate with applications that don't exist anymore (e.g. have been deleted).
    Edited by: GChierico on Apr 11, 2013 2:16 PM

  • How to send File with Same name to Receiver

    HI Frnds,
    In my peoject i have one scenario,
    I have to access Orders is the name of file , available in FTP Server, i have to send same file to Receiver side with same name Orders
    Is it possible with same name dynamically??
    Regards,
    Raj

    hi,
    You have to go for variable substitution option available in receiver file adapter,but for that you have to get the file name in receiver payload.
    http://help.sap.com/saphelp_NW04/helpdata/en/e3/94007075cae04f930cc4c034e411e1/frameset.htm
    these links are  also helpful
    /people/jayakrishnan.nair/blog/2005/06/20/dynamic-file-name-using-xi-30-sp12-part--i
    /people/jayakrishnan.nair/blog/2005/06/28/dynamic-file-namexslt-mapping-with-java-enhancement-using-xi-30-sp12-part-ii
    /people/sriram.vasudevan3/blog/2005/11/21/effective-xsl-for-multimapping-getting-source-filenames-in-legacy-legacy-scenarios
    regards,
    ujjwal kumar

  • Files with same name

    Hello,
    Is it possible to have several files with different assetId, but with the same assetUri, and in the same folder?
    Initially, I though yes, because the assetId is a unic id that could differentiate two asset with the same assetUri (that is the visible name in the user interface).  But after trying, I noticed that, in this situation, sometime only one asset is shown in the UI (bridge for example), and sometime all the assets are shown, but they are inaccessible (no preview, no possible to open them, no information, just the two assets with the same name represented in Bridge).
    Is there then a way to show, in the user interface, two assets (with different assetId) with a same name?
    Thank you
    ps: by the way, to have two files with same assetUri, different assetId and in the same directory, it makes Bridge struggling (using 100% processor)

    You can use the same class name in different packages.
    The Java API does that (java.awt.List, java.util.List).
    This is odd. What version of javadoc are you using,
    and what command are you passing in, with what classpath setting?
    -Doug Kramer
    javadoc team

  • Multiple files with same name--- automatic renaming option??

    I am trying to sort my files by adding multiple files to a single folder. However, many have the same filename, and I get the error " there already exists a file with the name "x", please choose another name" etc. I am dealing with thousands of files here that are very tedious to rename individually. Is there an option or program that either disables this block on multiple files with the same name in a common folder, or automatically renames the files as they are placed in the folder?

    i would like all the files to have different names, but not have to do it myself. they are generated my my audio recorder, which automatically names files take1, take2, take3 etc. multiple sessions entail multiple folders, i am trying to consolidate.

Maybe you are looking for

  • Unable to execute an update statement using CallableStatement

    Hi there, I'm trying to run an update statement from JUnit using java.sql.CallableStatement and oracle.jbo.server.DBTransaction.         String updateSql =             "update footable set barcol=TO_DATE('12-SEP-09','dd-MM-yy') where bazcol = 505";  

  • Delay in the change user password OIM 11g

    Hi guys, I have a problem with OIM 11g. The user accesses the OIM to do change your password, when the message that password is changed show, the user executed log-off and try access with new password, but the new password isn't accepted. The new pas

  • Does anyone know how to reduce KERNEL_TASK? PLEASE HELP!!!

    i am currently using MBR early 2011 15ing 2GHZ

  • A possible workaround for C2D Airport problems?

    A collegue advised the other day to download iStumbler, a free application that seems to sit and monitor all connections coming in your Airport, also for Bluetooth, Bonjour, etc. Well, I did... and ever since this thing started to work seeing my Netw

  • Purchase requisition, user-exit before save

    Hi, my need is very simple, when i save my purchase requisition i need to access in modification to items (standard fields) of my document. I'm looking for a few days on SDN or Google but without success, the question has often been discussed but the