Avatar of Vadim2004
Vadim2004
Flag for United States of America asked on

Copy file from local drive to web server

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";
            try{
                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);
                System.out.println(ioe.getMessage());
                res.sendRedirect("/servlet/errorMsg?error="+ error);
            }
         //Save file name to DB
         try{
            con = cp.getConnection();
 
            PreparedStatement statement = con.prepareStatement ("INSERT INTO ticket_attachment (ticket_num, tckt_attachment) VALUES (" + ticketNum + ", '" + fileName + "')");
 
            statement.close();
            cp.releaseConnection(con);
        }
        catch (Exception e){
            String error = "Database+error+while+saving+attachment+to+database+in+doPost+method:+" + e;
            System.out.println(getDate()+" "+error);
            System.out.println(e.getMessage());
            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);
            System.out.println(e.getMessage());
            res.sendRedirect("/servlet/errorMsg?error="+ error);
        }
        finally {
            if (inChannel != null) {inChannel.close();}
            if (outChannel != null) {outChannel.close();}
        }
   }
 
Java EEASPJava

Avatar of undefined
Last Comment
Vadim2004

8/22/2022 - Mon
Vadim2004

ASKER
BTW, i use <input type="file"> to chose file
cmalakar

>> /path/attachments/111111_021210030000_Test.txt";

What is this path ?

Is it a unix os path ?
Vadim2004

ASKER
yes this is path on unix web server
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Vadim2004

ASKER
Good morning experts, is any suggestion on it?
ASKER CERTIFIED SOLUTION
cmalakar

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Vadim2004

ASKER

Actually I am using <input type=file> , or it does not make a difference and I have to use File's input stream?