Link to home
Start Free TrialLog in
Avatar of Vadim2004
Vadim2004Flag 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";
                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();}
Avatar of Vadim2004
Flag of United States of America image


BTW, i use <input type="file"> to chose file
>> /path/attachments/111111_021210030000_Test.txt";

What is this path ?

Is it a unix os path ?
yes this is path on unix web server
Good morning experts, is any suggestion on it?
Avatar of cmalakar
Flag of India image

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial

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