Weblogic Server: Copy files to shared location within network using Java - 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

Related

Copy SQLITE file from download folder to my app folder ANDROID 11

before android 10 i can restore my database from any folder now when i create my fileinputstream and error becomes: /storage/emulated/0/Download/bd/bd.sqlite: open failed: EACCES (Permission denied), I dont understan how give permision with MediaStore
FileInputStream fis = new FileInputStream(bdOrigen);
OutputStream output = new FileOutputStream(PathDestino);
// transferir bytes al backup
byte[] buffer = new byte[1024];
int length;
while ((length = fis.read(buffer))>0){
output.write(buffer, 0, length);
}
// Close the streams
output.flush();
output.close();
fis.close();
No simple answer here, you will have to start reading up on Storage Access Framework as i think Scoped Storage will not meet your requirements

File Copy from local to remote system using java IO

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.

Apache Commons FTP storeFileStream returns null

I am trying to upload a file in android to an FTP server using the apache.commons.ftp library.
Note: I am using storeFileStream so that I can track the progress of the upload.
ftpClient.enterLocalPassiveMode();
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
InputStream inputStream = new FileInputStream(localFile);
OutputStream outputStream = ftpClient.storeFileStream(remoteFile);
byte[] buffer = new byte[8192];
int bytesRead;
logMessage("Starting to upload file");
while ((bytesRead = inputStream.read(buffer)) != -1) {
total += bytesRead;
outputStream.write(buffer, 0, bytesRead); //output stream is null here
latestPercentDone = (int) ((total / (float) totalMegaBytes) * 100);
if (percentDone != latestPercentDone) {
percentDone = latestPercentDone;
publishProgress(""+percentDone);
}
}
inputStream.close();
outputStream.close();
ftpClient.completePendingCommand();
I am using the speedtest.tele2.net server for testing. Using an anonymous login.
The log keeps saying the outputstream is null.
EDIT: Using what Martin Prikryl and getting the error code, i found the problem to be the location of the remote file.
Upload to a location rather than a directory.
For example if the file is caled item1.txt the remote file should be /item1.txt or /someFolder/AnotherFolder/item1.txt rather than /someFolder/AnotherFolder/
The FTPClient.storeFileStream method can return null:
An OutputStream through which the remote file can be written. If the data connection cannot be opened (e.g., the file does not exist), null is returned (in which case you may check the reply code to determine the exact reason for failure).
Though the "file does not exist" note is nonsense for upload, it's obviously a copy-and-paste error from the .retrieveFileStream (download).
Use the .getReplyCode and .getReplyString, to see what went wrong.

Uploading file to Openshift server with servlet

I'm not sure if this question has been asked before, but I could not find any resources on the internet answering my specific problem.
I am trying to upload a file from an Android app to my Openshift server/gear, where it will be stored. However, the issue I am facing is that whilst the file is being created at the Openshift side (I have checked using FTP), no data is being written to it.
The code snippet from the servlet that writes the data to the file is here:
int BUFFER_LENGTH = 4096;
DataInputStream din = new DataInputStream(req.getInputStream());
String fileName = din.readUTF();
String path = System.getenv("OPENSHIFT_DATA_DIR") + "/uploads/" + fileName + ".txt";
File f = new File(path);
FileOutputStream fos = new FileOutputStream(f);
byte[] buffer = new byte[BUFFER_LENGTH];
int length = 0;
while ((length = din.read(buffer, 0, BUFFER_LENGTH)) != -1) {
fos.write(buffer, 0, length);
}
fos.close();
din.close();
It all seems to be correct, to me at least, and it worked when I tested it on a local tomcat server. For some reason, however, it doesn't work with Openshift, so there must be something I am missing.
Luckily there is a help center article for just this issue:
https://forums.openshift.com/how-to-upload-and-serve-files-using-java-servlets-on-openshift
It details the code for both uploading, and serving files on OpenShift via a Java Servlet, using the openshift data directory

How to copy files over Network to LocalDrive in Java

I need a sample code that copies files over Network to Local File System in Java.
How this is done in Java?
here is code that copies files in the local file system
File fromfile = new File("file");
File tofile = new File("../copiedfile");
tofile.createNewFile();
FileInputStream from = new FileInputStream(fromfile);
FileOutputStream to = new FileOutputStream(tofile);
byte [] buffer = new byte[4096];
int bytesread;
while ((bytesread = from.read(buffer)) != -1) {
to.write(buffer, 0, bytesread);
}
I think, if you want to copy files over network you should send buffer using ObjectOutput and sockets

Categories

Resources