troubleshooting Question

Copy file from local drive to web server

Avatar of Vadim2004
Vadim2004Flag for United States of America asked on
Java EEASPJava
6 Comments1 Solution683 ViewsLast Modified:
Hello experts, I need your assistance:
  my web app has to copy over a file from local (or network) drive to web-server.
  file name will be saved to db.
Here is code - db part works fine file copy does not produce any errors but do not saves
file to location. Security on "/attachment" folder set to 775.
Please advise:

public void doPost (HttpServletRequest req, HttpServletResponse res)
        throws ServletException, IOException {
    String inputFile = "";
    String outputFile = "";
    ticketNum = 111111;
    String fileName = "111111_021210030000_Test.txt";
                inputFile  = "C:/Test.txt";  //req.getParameter("datafile").trim();
                outputFile = "/path/attachments/111111_021210030000_Test.txt";
                copyFile(inputFile, outputFile);
            catch(IOException ioe){
                String error = "FileCopy+error+while+retrieving+ticket+attachment:+" + ioe;
                System.out.println(getDate()+" "+error);
                res.sendRedirect("/servlet/errorMsg?error="+ error);
         //Save file name to DB
            con = cp.getConnection();
            PreparedStatement statement = con.prepareStatement ("INSERT INTO ticket_attachment (ticket_num, tckt_attachment) VALUES (" + ticketNum + ", '" + fileName + "')");
        catch (Exception e){
            String error = "Database+error+while+saving+attachment+to+database+in+doPost+method:+" + e;
            System.out.println(getDate()+" "+error);
            res.sendRedirect("/servlet/errorMsg?error="+ error);
private void copyFile(String fileIn, String fileOut) throws IOException
       FileChannel inChannel = null;
       FileChannel outChannel = null;
       try {
        File inputFile  = new File(fileIn);
        File outputFile = new File(fileOut);
        inChannel = new FileInputStream(inputFile).getChannel();
        outChannel = new FileOutputStream(outputFile).getChannel();
            inChannel.transferTo(0, inChannel.size(), outChannel);
        catch (IOException e) {
            error = getDate () + "InputOutput+error+while+saving+attachment+file+: +" + e;
            System.out.println(getDate() + " " + error);
            res.sendRedirect("/servlet/errorMsg?error="+ error);
        finally {
            if (inChannel != null) {inChannel.close();}
            if (outChannel != null) {outChannel.close();}

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 6 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 6 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros