File Copy from local to remote system using java IO - java

I need to copy a file from my local system to remote system, for this I'm using the following code:
public class Autohost {
public static void main(String[] args) throws IOException {
InputStream in = new FileInputStream(new File(
"C:\\Users\\Jainesh_Trivedi\\Desktop\\WAR\\AutohostDemo1_1145.war"));
File f = new File("10.87.74.191\\C$\\IVS_Code\\tomcat\\apache-tomcat-7.0.57\\webapps\\AutohostDemo1_1145.war");
f.createNewFile();
OutputStream out = new FileOutputStream(f);
// Transfer bytes from in to out
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();
}
}
But I'm getting the following error:
Exception in thread "main" java.io.IOException: The system cannot find the path specified
at java.io.WinNTFileSystem.createFileExclusively(Native Method)
at java.io.File.createNewFile(Unknown Source)
at com.autohost2.java.Autohost.main(Autohost.java:18)

The filename on this line
File f = new File("10.87.74.191\\C$\\IVS_Code\\tomcat\\apache-tomcat-7.0.57\\webapps\\AutohostDemo1_1145.war");
is not a valid UNC path. You need two backslashes (four, in code) to signal a remote path. Fixed version:
File f = new File("\\\\10.87.74.191\\C$\\IVS_Code\\tomcat\\apache-tomcat-7.0.57\\webapps\\AutohostDemo1_1145.war");
Also make sure that security settings on the remote machine are configured to allow your account the appropriate access.

Related

Weblogic Server: Copy files to shared location within network using Java

Our application is deployed on weblogic server 12c. This application needs to copy files from server to some folder on the network location.
How can this be achieved in Java?
Application code is like
String source =
"C:\Oracle\Middleware\Oracle_Home\user_projects\domains
\base_domain\pdf_files\ABC.pdf";//Location on server
String destination = "\\machineA\SharedFolder";//shared folder in some machine on same network
FileInputStream in = new FileInputStream(source);
FileOutputStream out = new FileOutputStream(destination);
byte[] buf = new byte[1024];
int len = 0;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();
Getting a error message
java.io.FileNotFoundException: \\machineA\SharedFolder\ABC.pdf (Access is denied)
machineA(server) can be added in the shared folder's Sharing options as machineA$.
Then this code which is running from application server will be able to access the location.
Reference: https://serverfault.com/questions/135867/how-to-grant-network-access-to-localsystem-account

Sysout list of files and transfer files from a directory in java

I'm trying to figure out how to get a list of files on the server and how to download them individually. Can anyone guide me in the right direction?
I'm getting file permissions error or (is a directory errors), thanks
UPDATE:
The error is
Exception in thread "main" java.io.FileNotFoundException: /myClientFiles (Permission denied)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(FileOutputStream.java:213)
at java.io.FileOutputStream.<init>(FileOutputStream.java:101)
at
xxxxxxpackegename.com.fr(Client.java:38)
Client.java
public class Client {
private static final int PORT = 2665;
private static String HOST = "localhost";
public static void main(String[] args) throws UnknownHostException, IOException {
int filesize = 5000000; //buffer size 5mb
int bytesRead;
int currentTotalNumberOfBytes = 0;
//connect to port on server - server waits for this after running socket.accept() in the Server class
Socket socket = new Socket(HOST, PORT);
byte[] byteArray = new byte[filesize]; //create a byte array of 5mb
InputStream inputStream = socket.getInputStream(); //channel to write to server
FileOutputStream fileOutStream = new FileOutputStream("/myClientFiles");
BufferedOutputStream bufferOutStream = new BufferedOutputStream(fileOutStream);
bytesRead = inputStream.read(byteArray, 0, byteArray.length);
currentTotalNumberOfBytes = bytesRead;
do { //read till the end and store total in bytesRead and add it to currentTotalNumberOfBytes
bytesRead = inputStream.read(byteArray, currentTotalNumberOfBytes, (byteArray.length-currentTotalNumberOfBytes));
if(bytesRead >= 0) currentTotalNumberOfBytes += bytesRead;
}while(bytesRead > -1); // when bytesRead == -1, there's no more data left and we exit the loop
bufferOutStream.write(byteArray, 0 , currentTotalNumberOfBytes); //write the bytes to the file
bufferOutStream.flush();
bufferOutStream.close();
socket.close();
}
}
Server.java
ServerSocket serverSocket = new ServerSocket(2665);
Socket socket = serverSocket.accept();
System.out.println("Connected to: " + socket);
//File transferFile = new File("Allcrisis.doc"); //get local file
File[] transferFiles = new File("/myServerFiles").listFiles(); //array to store pathnames of files in myServerFiles folder
byte[] bytearray = new byte[(int)transferFiles.length];
for(File file: transferFiles){
//FileInputStream fileInputStream = new FileInputStream(transferFiles);
FileInputStream fileInputStream = new FileInputStream(file);
BufferedInputStream butterInputStream = new BufferedInputStream(fileInputStream);
butterInputStream.read(bytearray, 0, bytearray.length);
OutputStream outStream = socket.getOutputStream();
System.out.println("Sending...");
outStream.write(bytearray, 0, bytearray.length);
outStream.flush();
}
socket.close();
You have a folder /myClientFiles.
Your code contains
new FileOutputStream("/myClientFiles")
which attempts to open a FileOutputStream to write to that folder. You can't write to a folder.
You might want to pass new FileOutputStream the path to the file to write to.

Reading data from multiple zip files and combining them to one

I want to read data from lets say 4 zip files called zip1, zip2, zip3, zip4. All of these zip files are split from this 1 big zip file called "BigZip". I want to combine the zip files into one and then compare the bytes if the 1 bigzip file matches the size of bytes with the combined zip file of (zip1+zip2+zip3+zip4). I am getting a very small file size when I combine the size of 4 zip files. What am I doing wrong?
Here is my code for the same:
targetFilePath1, targetFilePath2, targetFilePath3, targetFilePath4 belongs to path of 4 zip files.
sourceFilePath is the path to BigZip file
class Test {
public static void main(String args[]) {
ZipOutputStream outStream = new ZipOutputStream(new FileOutputStream(sourceBigZip));
readZip(sourceFilePath, targetFilePath1);
readZip(sourceFilePath, targetFilePath2);
readZip(sourceFilePath, targetFilePath3);
readZip(sourceFilePath, targetFilePath4);
outStream.close();
}
static void readZip(String sourceBigZip, String targetFile) throws Exception {
ZipInputStream inStream = new ZipInputStream(new FileInputStream(targetFile));
byte[] buffer = new byte[1024];
int len = inStream.read(buffer);
while (len != -1) {
outStream.write(buffer, 0, len);
len = inStream.read(buffer);
System.out.print(len);
}
inStream.close();
}
}
Create ZipOutputStream once and pass it to readZip() method, like:
public static void main(String args[]) {
ZipOutputStream outStream = new ZipOutputStream(new FileOutputStream(sourceFilePath));
readZip(outStream , targetFilePath1);
readZip(outStream , targetFilePath2);
readZip(outStream , targetFilePath3);
readZip(outStream , targetFilePath4);
}
Then you have an error dealing with copying the data from one zip to another...
You need to copy each file in the zip file like this:
static void readZip(ZipOutputStream outStream, String targetFile)
throws Exception {
ZipInputStream inStream = new ZipInputStream(new FileInputStream(
targetFile));
byte[] buffer = new byte[1024];
int len = 0;
for (ZipEntry e; (e = inStream.getNextEntry()) != null;) {
outStream.putNextEntry(e);
while ((len = inStream.read(buffer)) > 0) {
outStream.write(buffer, 0, len);
}
}
inStream.close();
}
}
Every time you call new ZipOutputStream, it creates a new empty file, and wipes out everything you have written to it before.
You have to create the stream outside of readZip, and pass it in to each call rather than creating a new stream every time.

Replace a specific file in a zip using java

I need to replace a specific CSS file inside zip archive the file is stored under folders EPUB/styles/stylesheet.css
here is my code(I need to do it in java 6)
public static void main(String[] args) throws IOException {
File newCss=new File("D:\\test\\css\\stylesheet.css");
ZipOutputStream out = new ZipOutputStream(new FileOutputStream
(new File("D:\\test\\css\\edited\\my_book.epub"),true));
out.putNextEntry(new ZipEntry("EPUB/styles/stylesheet.css"));
InputStream in = new FileInputStream(newCss);
byte[] buf = new byte[4096 * 1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
out.closeEntry();
in.close();
out.close();
System.out.println("Done Replacing entry");
}
}
but on executing this code the whole zip contains only the CSS i replaced all the other contents are lost but the zip shows the same size as before, on extracting the zip i get only the file i replaced.

java.io.FileNotFoundException (Access is Denied) in a static method to move file

I was met with a java.io.FileNotFoundException: C:\Users\520\Desktop\Thing (Access is denied) error when running the following script to move files. Does this mean I should run my IDE under admin privileges?
public static void moveFiles(){
InputStream inStream = null;
OutputStream outStream = null;
try{
File afile = new File("C:\\Users\\520\\Desktop\\hey.txt"); // Gotta specify initial path. Consider adding an input for this
File bfile = new File("C:\\Users\\520\\Desktop\\Thing");
inStream = new FileInputStream(afile);
outStream = new FileOutputStream(bfile);
byte[] buffer = new byte[1024];
int length; // copy the file content in bytes
while((length = inStream.read(buffer)) > 0){
outStream.write(buffer, 0, length);
}
inStream.close();
outStream.close();
afile.delete();
System.out.println("File was copied successfully!");
}catch(IOException e){
e.printStackTrace();
}
}
Use this to adjust the destination file object so that if it is a directory, it will instead use the source file's name within that directory.
if (bfile.isDirectory()) bfile = new File(bfile, afile.getName());

Categories

Resources