Using a thread to write a file from a servlet

Does anyone have some code that can help out my problem:
I am writing a file from a servlet, but the servlet completes and loads another page prior to the file writing to disk being completed.
I have tried using threads to stop the serlvet until the file has been completely written, but I am not experianced using threads properly, and so far have been unsuccesssful.
I would sure appreciate some/any code to reference to overcome this problem.
Thanks to any responses.
Dale

Hi the best idea for you is not to use thread, so that the file processing gets completed before your servlet start doing anything else..
something like this.
public void doGet(     HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException
System.out.println("Called Repo");     
BufferedInputStream res = new BufferedInputStream((request.getInputStream()));
     File f= new File("img.jpeg");
     FileOutputStream fo = new FileOutputStream(f);     
          byte b[]=new byte[64];
          int line=res.read(b);
while(line!=-1)
     fo.write(b);
     line=res.read(b);
fo.close();
//your remaining processing from here...
regards

Similar Messages

  • Db- put blocks sometimes, mulit-thread uses Db- put to write DB file

    My environment : redhat Linux 5, gcc version 4.1.2
    BDB version: 4.8.24
    Hi Guys
    I used multi-thread application to write DB file. The application used Transactional Data Store and I used flags DB_TXN_NOSYNC . The DB file type is BTREE. I give 180M for the buffer cache and 8M for the log cache. And I used Db->put to write DB file ; five threads is used.
    But the Db->put will block for one or two seconds sometimes.
    Now I have found that the problem is in the  lock/lock.c :: __lock_get_internal , line 953 :  MUTEX_LOCK(env, newl->mtx_lock);. My application blocks here .+_
    Could anyone give me some advice to avoid these blocks as possible as we can?
    By the way, I have tried many of the lock set APIs which BDB offered. No effect -_- .
    Thanks

    Hi,
    You should have posted this new piece of information in the original thread you opened:
    Db->put block sometimes , when mulit-thread uses Db->put to write DB file
    In the future please do not open a new thread for a problem you already reported.
    Now I have found that the problem is in the  lock/lock.c :: __lock_get_internal , line 953 :  MUTEX_LOCK(env, newl->mtx_lock);. My application blocks here .+_This only confirms that one of your threads is blocked waiting for a requested lock, which is already held by another thread. Until the thread that holds the lock will not have released it, your thread waiting on it will not make progress.
    You can collect the locking statistics at the moment you observe the slowness using either the db_stat utility with the -CA or -Co options (if the environment is not opened with DB_PRIVATE, that is, backing its region files into per-process private/heap memory) or programmatically using DB_ENV->lock_stat_print():
    [http://www.oracle.com/technology/documentation/berkeley-db/db/api_reference/C/db_stat.html]
    [http://www.oracle.com/technology/documentation/berkeley-db/db/api_reference/C/lockstat_print.html]
    Examining the locking statistics will help determine what is the problematic lock, what is the locked object and which thread is holding it.
    In addition to standard deadlock detection you can look into configuring lock timeouts. If your lock request is not satisfied within a given time it will time out and return DB_LOCK_DEADLOCK or DB_LOCK_NOTGRANTED. Lock timeouts may be configured per environment wide basis or per-lock or per-transaction basis:
    [http://www.oracle.com/technology/documentation/berkeley-db/db/programmer_reference/lock_timeout.html]
    [http://www.oracle.com/technology/documentation/berkeley-db/db/gsg_txn/C/lockingsubsystem.html#configdeadlkdetect]
    As Mike already suggested it would help if you could put together a test case program to demonstrate this issue.
    Regards,
    Andrei

  • PROBLEM while using URL in order to open a file from a servlet

    Hi,
    I am having a problem while trying to open a file from my servlet by using URL connection. Following is my code sample. what happens is when I first run my project it opens the file but after that when I rerun it again and again it sometimes opens it and sometimes not. if it can't open it it gives an error saying "the file is damaged and couldn't be repaired". I don't get any exceptions. if you guys can help me I will appreciate it so much... thanks.
    here is my code :
    URL url = new URL("http://demobcs.ibm.com:81/test.pdf");
    resp.setContentType(getContentType("http://demobcs.ibm.com:81/test.pdf"));
              ServletOutputStream sos = null;
              BufferedInputStream bis = null;
              try {
                   sos = resp.getOutputStream();
              } catch (Exception e) {
                   System.out.println("exception !!!!");
                   e.printStackTrace();
              System.out.println("burada2");
              try {
                   byte[] bytes = new byte[4096];
                   //bis = new BufferedInputStream(conn.getInputStream());
                   DataInputStream in = new DataInputStream(url.openStream());
                   //DataInputStream in = new DataInputStream(conn.getInputStream());
                   bis = new BufferedInputStream(in);
                   int j;
                   while ((j = bis.read(bytes, 0, 4096)) != -1) {
                        try {
                             sos.write(bytes, 0, 4096);
                             System.out.println("file is being read ...:");
                        } catch (Exception e) {
                             e.printStackTrace();
                   in.close();
                   bis.close();          
                   //sos.close();
              } catch (Exception e) {
                   System.out.println("exception :"+e.toString());
                   e.printStackTrace();
              }

    You are writing (<filesize> mod 4096) bytes of crap at the end of the file. Use your variable j instead for the number of bytes to write in your loop.

  • I used to be able to download files from the Harddrive of my Sony Handycam, but now it won't let me import them while going through "log and transfer."  I have tried to update my settings, but this doesn't seem to work.  Any help would be much appreciated

    I used to be able to download files from the Harddrive of my Sony Handycam, but now it won't let me import them while going through "log and transfer."  I have tried to update my settings, but this doesn't seem to work.  Any help would be much appreciated

    Hard Drive:  The Hard Drive is on my camera (Sony Handycam)
    Just needed some info on what is going on with your particular system.
    Knowing the exact camera model will also assist us.
    If this camera is standard AVCHD you should be able to connect with USB to Mac with FCE open and use Log and Transfer without all the fiddling around converting etc.
    What FCE does during ingest is convert files to AIC. (Apple Intermediate Codec)
    FCE cannot read the individual files in the BDMV folder, that's why all the converting is required when you use that method.
    BTW: regards formatting drives/cards/memory on cameras; it is wise to use the actual device to format rather than a computer. This is a prime cause of read/write goof ups with solid state media. This applies to still or video cameras.
    Keeping the COMPLETE card/memory structure in tact is vital for successful transfers.
    Try here for some trouble shooting tips:
    https://discussions.apple.com/message/12682263#12682263
    Al

  • Write HTML Files from java application

    Hello,
    i have to write HTML files from a java application.
    I am asking if anybody can give me a hint about a good simple solution (library) which can do some of the job for me.
    i know FOP for example is good to create pdf or html from xml, but i dont want to go via xml.
    the sites i need are really very simple presents emails.
    body and maybe a link for next and pervious.
    however, thank you in advance.
    a little hint will also help :)
    Sako.

    That, too, is my question.
    Be the "server" local or remote, wouldn't JSPs still
    be the easier solution? No. it will not. because you need a server. Server for a stand alone application is not esier. according to who said JSPs are easy?
    Its very very difficult.
    >
    When answering, please clarify. I'm a bit of a newbie
    here. :)To get sence about how hard is JSP, check Struts. this is very good open source Framework to simplify the JSPs. but it still complecated enough.
    or Tapestry my lovely open srouce Framework.
    its easier than Struts. but sill complecated because of the documentation.
    All in all, using JSP is the purpose of Java in the internet. but not for me. My application will not be available in the internet, i.e. no server, i.e. no need for JSP.
    i hope that helps a little.

  • Using Migration Assistant to Move Data Files from one Account to New User

    Is it possible or recommended to use Migration Assistant to move data files from an old user account to a new user account on the same volume/hard disk?

    Migration Assitant cannot be used as a simple file transfer utility. MA can transfer an entire Home folder or entire Users folder but not individual files.
    In general files in one user account cannot be moved to another user account as this would violate built-in security measures.
    What you can do is to copy the files to an external device like a hard drive or flash drive. First log in to the old account. Then configure the drive to ignore preferences by Pressing COMMAND-I to open the Get Info window and check the Ignore permissions on this volume checkbox at the bottom of the window. Copy your files to the device. Log into the new account. Verify the external drive is still configured to ignore permissions (reset it, if not.) Copy the files from the external device into the Home folder of the new account. I would create a temporary folder for these files. If you still end up without the proper permissions you will need to make modifications that are easier done if all the files are in a single folder.

  • Im having problem with the dvd quality, I'm using compressor to convert the video fils from apple prores to mpeg2 .It doesn't matter how long my video is even if its just 5 minutes  I'm getting cut edges in the video , does anyone have any idea ?

    Im having a problem with the dvd quality, I'm using compressor to convert the video fils from apple prores to mpeg2 .It doesn't matter how long my video is even if its just 5 minutes  I'm getting cut edges/lines  in the video specialy if i have titles it comes up really bad , I took the same video to a friend of mine who have PC and he uses Encore , did the encoding there and it was just fine no problems! BTW I tried using doferent setings in compressor from CBR and VBR I even pushed up the setings to 8 or 9 BR and still no luck !
    does anyone have any idea ?
    Thanks in advence ...

    Let's focus attention on just the Sony. (What model and what resolution are you shooting?)
    For now, I'll assume you're shooting 1080i.
    Take a representaive clip  into a new sequence. Add a title.
    In your sequence, make sure field dominance is set to Upper.
    Set render settings to Pro Res 422.
    After rendering, export QT self contained.
    Import into Compressor (I'm now referring to v3.5).
    Select the 90 minute Best quality DVD preset.
    Open frame controls and turn on (click the gear icon). Set Resize filter to Best.
    Submit burn and check quality on TV.
    Good luck.

  • MacBook Pro i5 with retina will not recognize WD Passport even though I successfully used it to transfer programs and files from my older MacBook ten days ago. It receives power from the USB, but when using Time Machine I receive the message that there is

    MacBook Pro i5 with retina will not recognize WD Passport even though I successfully used it to transfer programs and files from my older MacBook ten days ago. It receives power from the USB, but when using Time Machine I receive the message that there is not an external device connected.

    If the modem is also a router, either use the modem in bridge and run pppoe client on the TC.. that is assuming ADSL or similar eg vdsl. If it is cable service.. and the modem is a router, then bridge the TC.. go to internet page and select connect by ethernet and below that set connection sharing to bridge.
    Please tell us more about the modem if the above gives you issues.

  • What is the best way to use wi-fi to transfer pdf files from an ipad to a mac mini, not using an internet, just wi-fi.

    What is the best way to use wi-fi to transfer pdf files from an ipad to a mac mini, not using an internet, just wi-fi?

    If the Mac use Lion (10.7) you can use Airdrop for that.
    With Mac's not using Lion you may set up a computer to computer (ad hoc) connection.
    How to --> http://docs.info.apple.com/article.html?path=Mac/10.6/en/8339.html
    Lupunus

  • I just used a firewire connection to transfer files from an older MAC to a newer MAC. My newer MAC has lost all that was on it, and all the applications are opening like they have never been used before and asking for Rosetta. Help!!!

    I just used a firewire connection to transfer files from a PowerBook G4 to a MacBook Pro. The MacBook Pro has lost all that was on it, and all the applications are opening like they have never been used before and asking for Rosetta. How do I find all my files on the MacBook Pro, or undo what I have done???

    IGNORE the last post: he thought you are using Lion or Mountain Lion.
    Since you are using Snow Leopard, you need to install Rosetta.  It can be found on your Snow Leopard Install DVD in the Options section.
    Once installed all should be well again.
    Also, if your computer is connected to the internet when the Dialog Box about PowerPC applications comes up, it MIGHT allow you to download Rosetta from the internet.  There has been some discussion that Apple has discontined internet download of Rosetta into Snow Leopard.
    Someone else will have to address your lost files problem.

  • Executing .sh files from a servlet

    Hi,
    Please clarify me on executing a .sh files from a servlet. or from a java class.
    Thanks.

    Runtime rt = Runtime.getRuntime();
    rt.exec(" <u r shell script file >");
    this works fine in java class... i dont exactly know weather it works in servlet or not.... check it out....
    jogesh

  • Can i use windows migration assistant to transfer files from my windows formatted external hard drive to my mac?

    I have a windows formatted external hard drive and a macbook pro. I was wondering if i could use the windows migration assistant to transfer files from my external hard drive to the mac

    No.
    Just connect the drive to your Mac. The Mac can read most Windows formats (it cannot WRITE Windows New Technology File System (NTFS) formatted drives.

  • HELP using a fire wire to tranfer files from my PC to my new MacBook

    I just purchased a new MacBook and am trying to transfer my songs, pictures and movies from my desktop pc (windows XP) to my new MacBook. I was instructed to use a 6pin to 6pin firewire to tranfer the files from my pc to my MacBook by using the MacBook as an external hard drive. I can't seem to get my PC to recognize my macBook as an external hard drive. I have connected both firwire inputs to both computers. I have put my MacBook into firewire transfer mode by holding down "T" at the startup. When I check my PC and open my computer, I see no signs of my mMacBook. According to other discussions I have read, the MacBook should show up on the pc and I should be able to just click and drop the files into the macBook. What am I doing wrong and what should I do to successfully transfer these files from the pc to the macbook?

    Not sure who told you to do this but your PC cannot read or write Mac drives. Unless you have third-party software on the PC such as MacDrive from Media Four you will not be able to transfer files this way. The following articles may help you:
    http://docs.info.apple.com/article.html?artnum=75320
    http://www.apple.com/support/switch101/

  • HT4796 Can i use windows migration assistant to transfer files from a windows formatted hard drive to a mac?

    I have a windows formatted hard drive and a macbook pro. I was wondering if i could use the windows migration assistant to transfer files from my hard drive to the mac

    No.
    Just connect the drive to your Mac. The Mac can read most Windows formats (it cannot WRITE Windows New Technology File System (NTFS) formatted drives.

  • Write PDF files from database to file system

    Hi,
    I have a requirement of writing PDF files that are stored in Oracle 10g's BLOB column to the FILE SYSTEM using PL/SQL (Not java or any external stored Proc), since i need to write a shell script which will load the PDF files from database on a timely basis.
    Could anyone suggest me the way of executing this?
    Environment: Oracle 10G (10.2.0.1.0) with Windows 2003 server.
    Regards,
    Nagarjun.

    You could try to use the UTL_FILE package that is able to read/write text and binary data in a file located on the box hosting the database instance.

Maybe you are looking for