Results 1 to 14 of 14

Thread: Copying files

  1. #1
    Join Date
    Dec 2011
    Location
    Earth
    Posts
    8
    Rep Power
    0

    Default Copying files

    Hi,

    I am trying to export selected file from /home/user/ejbca/p12/pem folder to /home/user/Desktop/certs folder using a button in a web application. What would be the best way to do this?

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  3. #3
    Join Date
    Dec 2011
    Location
    Earth
    Posts
    8
    Rep Power
    0

    Default Re: Copying files

    I am trying to use a jsp page to copy certain files from one location to another with the click of a button. I tried
    Java Code:
    String cmd = "cp /home/user/ejbca/p12/pem/"+usernames[i]+".pem"+" /home/user/Desktop/certs/"; 
    Process proc = Runtime.getRuntime().exec(cmd,null,null);
    but it didn't work. Is there another way to do this?

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default Re: Copying files

    When comes to JSP, there are no cleaner way to copy files. However, Java I/O classes are enough powerful to write your own file copy.

    With the use of File class initialize two handles for the source and destination. Same as using FileInputStream and FileOutputStream initialize streams for the job. Define a byte array to hold data (with the preferable size, smaller the better) and write to the destination through the source. You may do some validations like the destination folder path has write permissions and so on.

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default Re: Copying files

    Quote Originally Posted by Scarlet Sphere View Post
    but it didn't work. Is there another way to do this?
    Did you come across any exception or like that?

  6. #6
    Join Date
    Dec 2011
    Location
    Earth
    Posts
    8
    Rep Power
    0

    Default Re: Copying files

    Quote Originally Posted by Eranga View Post
    When comes to JSP, there are no cleaner way to copy files. However, Java I/O classes are enough powerful to write your own file copy.

    With the use of File class initialize two handles for the source and destination. Same as using FileInputStream and FileOutputStream initialize streams for the job. Define a byte array to hold data (with the preferable size, smaller the better) and write to the destination through the source. You may do some validations like the destination folder path has write permissions and so on.
    I'm not really sure how to use FileInputStream and FileOutputStream to copy files.

  7. #7
    Join Date
    Dec 2011
    Location
    Earth
    Posts
    8
    Rep Power
    0

    Default Re: Copying files

    Quote Originally Posted by Eranga View Post
    Did you come across any exception or like that?
    I got an error that said "cp" and cannot allocate memory. Something like that.

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  9. #9
    Join Date
    Dec 2011
    Location
    Earth
    Posts
    8
    Rep Power
    0

    Default Re: Copying files

    I tried it but I am still unsure. I tried the file reader example and got:
    Java Code:
    Compiling 1 source file to /home/cbis/workspace/ejbca/modules/admin-gui/tmp-jasper/classes
        [javac] /home/cbis/workspace/ejbca/modules/admin-gui/tmp-jasper/jspc/org/apache/jasper/jspc/ra/listendentities_jsp.java:365: illegal start of expression
        [javac]                 throws IOException {
        [javac]                 ^
        [javac] /home/cbis/workspace/ejbca/modules/admin-gui/tmp-jasper/jspc/org/apache/jasper/jspc/ra/listendentities_jsp.java:365: not a statement
        [javac]                 throws IOException {
        [javac]                        ^
    and 18 others. Is it not possible to just use runtime?

  10. #10
    Join Date
    Dec 2011
    Location
    Earth
    Posts
    8
    Rep Power
    0

    Default Re: Copying files

    .....

  11. #11
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  12. #12
    Join Date
    Dec 2011
    Location
    Earth
    Posts
    8
    Rep Power
    0

    Default Re: Copying files

    Ant build was successful until I put the code in the middle which I got fromByte Streams (The Java™ Tutorials > Essential Classes > Basic I/O)
    Java Code:
    for(int i = 0; i < indexes.size(); i++){
                    index = ((java.lang.Integer) indexes.elementAt(i)).intValue();
                    usernames[i] = java.net.URLDecoder.decode(request.getParameter(HIDDEN_USERNAME+index),"UTF-8");
                    name=usernames[i];
    Added code:
    Java Code:
    throws IOException {
    
            FileInputStream in = null;
            FileOutputStream out = null;
            try {
                in = new FileInputStream(
                             "xanadu.txt");
                out = new FileOutputStream(
                             "outagain.txt");
                int c;
    
                while ((c = in.read()) != -1) {
                    out.write(c);
                }
    
            } finally {
                if (in != null) {
                    in.close();
                }
                if (out != null) {
                    out.close();
                }
            }
    Java Code:
    }
    When I remove the "throws IOException", it gives another error:
    Java Code:
    [javac] Compiling 1 source file to /home/cbis/workspace/ejbca/modules/admin-gui/tmp-jasper/classes
        [javac] /home/cbis/workspace/ejbca/modules/admin-gui/tmp-jasper/jspc/org/apache/jasper/jspc/ra/listendentities_jsp.java:5197: 'catch' without 'try'
        [javac]     } catch (Throwable t) {
        [javac]       ^
        [javac] /home/cbis/workspace/ejbca/modules/admin-gui/tmp-jasper/jspc/org/apache/jasper/jspc/ra/listendentities_jsp.java:5197: ')' expected
        [javac]     } catch (Throwable t) {
        [javac]                       ^
    and 15 others. I'm not sure what is wrong here.

  13. #13
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  14. #14
    Join Date
    Dec 2011
    Location
    Earth
    Posts
    8
    Rep Power
    0

    Default Re: Copying files

    Its too long to post the complete code but this is the part which I am modifying:
    Java Code:
    if( request.getParameter(BUTTON_EXPORT_CERTS) != null){
        	  editbuttonpressed=true;
              java.util.Enumeration parameters = request.getParameterNames();
              java.util.Vector indexes = new  java.util.Vector();
              int index;
              while(parameters.hasMoreElements()){
               String parameter = (String) parameters.nextElement();
                if(parameter.startsWith(CHECKBOX_SELECT_USER) && request.getParameter(parameter).equals(CHECKBOX_VALUE)) {
                  index = java.lang.Integer.parseInt(parameter.substring(CHECKBOX_SELECT_USER.length())); //Without []
                  indexes.addElement(Integer.valueOf(index));
                }
              }
              String name = "";
              //Process proc;
              if(indexes.size() > 0){
                  String[] usernames = new String[indexes.size()];
                  for(int i = 0; i < indexes.size(); i++){
                    index = ((java.lang.Integer) indexes.elementAt(i)).intValue();
                    usernames[i] = java.net.URLDecoder.decode(request.getParameter(HIDDEN_USERNAME+index),"UTF-8");
                    name=usernames[i];
    //////////////////////////////////////Added
                    throws IOException {
    
                     FileInputStream in = null;
                     FileOutputStream out = null;
                     try {
                       in = new FileInputStream(
                             "xanadu.txt");
                       out = new FileOutputStream(
                             "outagain.txt");
                       int c;
    
                       while ((c = in.read()) != -1) {
                           out.write(c);
                       }
                     }      
                    finally {
                       if (in != null) {
                         in.close();
                       }
                       if (out != null) {
                         out.close();
                       }
                    }
    /////////////////////////////////////            
                }
            }
              //String cmd = "cp /home/cbis/ejbca/p12/pem/"+"User2.pem"+" /home/cbis/Desktop/certs/"; 
              //Process proc = Runtime.getRuntime().exec(cmd,null,null);
          }

Similar Threads

  1. Copying large files efficiently
    By bayan in forum New To Java
    Replies: 2
    Last Post: 10-27-2010, 06:01 PM
  2. add to a zip file without copying it.
    By yurabita in forum New To Java
    Replies: 5
    Last Post: 08-21-2010, 04:16 AM
  3. Copying Files
    By tgns223 in forum New To Java
    Replies: 13
    Last Post: 06-12-2010, 04:15 AM
  4. Copying a derectory
    By linux1man in forum New To Java
    Replies: 10
    Last Post: 01-15-2009, 08:16 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •