Results 1 to 14 of 14
Thread: Copying files
- 12-14-2011, 06:55 AM #1
Member
- Join Date
- Dec 2011
- Location
- Earth
- Posts
- 8
- Rep Power
- 0
- 12-14-2011, 07:54 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Re: Copying files
It is a file uploader then.
What are the technologies are you using?
- 12-14-2011, 08:02 AM #3
Member
- Join Date
- Dec 2011
- Location
- Earth
- Posts
- 8
- Rep Power
- 0
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
but it didn't work. Is there another way to do this?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);
- 12-14-2011, 08:16 AM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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.
- 12-14-2011, 08:17 AM #5
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Re: Copying files
- 12-14-2011, 08:22 AM #6
Member
- Join Date
- Dec 2011
- Location
- Earth
- Posts
- 8
- Rep Power
- 0
- 12-14-2011, 08:24 AM #7
Member
- Join Date
- Dec 2011
- Location
- Earth
- Posts
- 8
- Rep Power
- 0
- 12-14-2011, 08:28 AM #8
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Re: Copying files
- 12-14-2011, 09:57 AM #9
Member
- Join Date
- Dec 2011
- Location
- Earth
- Posts
- 8
- Rep Power
- 0
Re: Copying files
I tried it but I am still unsure. I tried the file reader example and got:
and 18 others. Is it not possible to just use runtime?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] ^
- 12-14-2011, 10:12 AM #10
Member
- Join Date
- Dec 2011
- Location
- Earth
- Posts
- 8
- Rep Power
- 0
Re: Copying files
.....
- 12-14-2011, 11:25 AM #11
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Re: Copying files
Check what you have written on
listendentities_jsp.java:365
- 12-15-2011, 03:24 AM #12
Member
- Join Date
- Dec 2011
- Location
- Earth
- Posts
- 8
- Rep Power
- 0
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)
Added code: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];
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(); } }When I remove the "throws IOException", it gives another error:Java Code:}
and 15 others. I'm not sure what is wrong here.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] ^
- 12-15-2011, 03:37 AM #13
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Re: Copying files
Is it possible to post the complete code here?
- 12-15-2011, 04:16 AM #14
Member
- Join Date
- Dec 2011
- Location
- Earth
- Posts
- 8
- Rep Power
- 0
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
-
Copying large files efficiently
By bayan in forum New To JavaReplies: 2Last Post: 10-27-2010, 05:01 PM -
add to a zip file without copying it.
By yurabita in forum New To JavaReplies: 5Last Post: 08-21-2010, 03:16 AM -
Copying Files
By tgns223 in forum New To JavaReplies: 13Last Post: 06-12-2010, 03:15 AM -
Copying a derectory
By linux1man in forum New To JavaReplies: 10Last Post: 01-15-2009, 07:16 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks