Create file on another server - java

How would I create a file in another windows server? The server has a username, and password, ip address and specific directory.

SAMBA! Braziiillll, Braziiiiiiillll!
Something like this:
String userPass = "username:password";
String filePath = "smb://ip_address/shared_folder/file_name";
NtlmPasswordAuthentication authentication = new NtlmPasswordAuthentication(userPass);
SmbFile smbFile = new SmbFile(filePath, authentication);
SmbFileOutputStream smbFileOutputStream = new SmbFileOutputStream(smbFile);
PrintStream printStream = new PrintStream(smbFileOutputStream);
//You should be good from this point on...
NOTE: The destination folder needs to be shared first!

As #orm already points out, this is answered already here FTP upload via sockets
Basically, you could reuse an existing library like Apache Commons Net to do it. For the specifics of using the FTP client have a look at the documentation for the class FTPClient class.

Related

Java file upload to windows shared folder with authentication

I'm trying to upload some files on a shared folder where I'm granted on with full control, but I see that connection is not working for some authentication reasons. This is the piece of code I've used for some writing tests:
String destination = "serverX/shareFolder/";
String domain = "myDomain";
String smbFile = "smb://"+domain+"/user1:pwd1#"+destination;
SmbFile sFile = new SmbFile(smbFile);
SmbFileOutputStream sfos = new SmbFileOutputStream(sFile);
sfos.write("Test".getBytes());
sfos.close();
this is the error I've received:
jcifs.smb.SmbAuthException: The referenced account is currently locked out and may not be logged on to.
Is looks like the code is good, the problem seems to be the account you are using. Have you tried other account? or maybe you can "unlock" the account someway at the server.

How to get an excel file from AWS S3 bucket into a MultipartFile in Java

I've been trying to extract an .xlsx file from a AWS bucket I created and store it as a multipartfile variable. I've tried many different approaches, but at best I get weird characters. I'm not finding much documentation on how to do this.
Thanks!
// you may need to initialize this differently to get the correct authorization
final AmazonS3Client s3Client = AmazonS3ClientBuilder.defaultClient();
final S3Object object = s3Client.getObject("myBucket", "fileToDownload.xlsx");
// with Java 7 NIO
final Path filePath = Paths.get("localFile.xlsx");
Files.copy(object.getObjectContent(), filePath);
final File localFile = filePath.toFile();
// or Apache Commons IO
final File localFile = new File("localFile.xlsx");
FileUtils.copyToFile(object.getObjectContent(), localFile);
I'm not 100% sure what you mean by "MultipartFile" - that's usually in the context of a file that's been sent to your HTTP web service via a multipart POST or PUT. The file you're getting from S3 is technically part of the response to an HTTP GET request, but the Amazon Java Library abstracts this away for you, and just gives you the results as an InputStream.

Java connect to FTP server and download files with java default libraries

I need to connect to an FTP server and browse all files without using any libraries like apache.commons because I don't have the option to get these libraries at the moment.
I tried using a simple URL connection:
URL url = new URL("username:password#ip/folder/");
URLConnection conn = url.openConnection();
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ( (line = reader.readLine()) != null ) {
System.out.println(line);
}
reader.close();
When I don't include the /folder/ it works, but it prints me many things that I can't even see in the home location of the FTP server, I don't know if it gives me the files or other data.
But when I do include /folder/ i get error CWD /folder/:550 failed to change directory
and I cannot change the directoy's permissions, its read-only and thats what I need.
What is wrong with it? Is it possible to do with just java's default libraries?
First, you need to use an FTP URL:
URL url = new URL("ftp:username:password#ip/folder/");
assuming username and password are substituted with their correct values.
Second, if you have FTP access to folder it will deliver you a directory listing in some format. If you don't, you need to study the exception message you get. If you omit /folder it will give you a listing of whatever the FTP server's default root directory is for that username. The code 550 means either an access problem or that the directory doesn't exist.

how to use common vfs to read excel from http server which displays authentication pop up in the browser?

I am new to common vfs. I am trying to read an excel sheet located on http://starpoint.com/...
Whenever i use this url on browser, it gives me authentication required pop up.I tried using the above code but it didn't work.
StaticUserAuthenticator auth = new StaticUserAuthenticator("domain", "username", "password");
FileSystemOptions opts = new FileSystemOptions();
DefaultFileSystemConfigBuilder.getInstance().setUserAuthenticator(opts, auth);
FileObject fo = VFS.getManager().resolveFile("htttp://starpoint.com/../", opts);
I also tried entering username and password in the url by this format http://username:password#starpoint.com/..../ but it does not work.
One more thing if my username is xyz\john then will xyz be my domain in the first line of the code? Any other way will also be appreciated.

how to send attachment using sendmail

I am use this class to send mail it works but now i wants to add attachment how to add it please help if u can
public static void sendMail(String subject, String body, String toEmail, String ccEmail, String fromMail)
throws IOException {
Random generator = new Random();
int r = Math.abs(generator.nextInt());
body = body.replaceAll("(\\r|\\n)", "");
body = body.replaceAll("\"", "\\\\\"");
body = body.replaceAll("&", "\\\\&");
body = body.replaceAll("©", "\\\\©");
//body = body.replaceAll("> <", ">\\\n<");
if(CommonUtils.emptyString(fromMail))
fromMail = "No Reply <iotasol#pcc.com>";
else
fromMail = "No Reply <"+fromMail+">";
ProcessBuilder processBuilder = new ProcessBuilder(
ApplicationProperties.MAIL_SENDER_SH_PATH, CommonUtils.getEmptyStringForNull(subject), CommonUtils.getEmptyStringForNull(body),
toEmail, ccEmail, String.valueOf(r), fromMail);
processBuilder.start();
}
as an idea if you need to send images: make it inline with base64.
Attachments depends on library used, mail server used and so on.
MimeBodyPart messageBodyPart = new MimeBodyPart();
File file = new File("somefile.txt");
if (file.exists()) {
DataSource source = new FileDataSource("somefile.txt");
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(file.getName());
multipart.addBodyPart(messageBodyPart);
}
From your code example, I can tell that you are using external mail program for sending emails. You create a ProcessBuilder and invoke an OS tool for sending emails.
I would not use this solution. First, it depends on the OS (on windows you don't have the mail command). Second, this is not efficient; since you create external process for this (imagine sending many emails).
Instead, try using existing mail solution in java (you will need: mail.jar and activation.jar). With it you can send emails directly from your application, not depending on external tool.
While with mail Java library you can do everything you want, you may also look at Jodd Email. This is a small, but convenient wrapper over java mail library, that can help you with sending emails and attachments. As you can see in section 'Email using fluent API', you can do the following:
Email email = Email.create()
.from("from#foo.org")
.to("to#bar.com")
.subject("test")
.addText("Hello!")
.addHtml(
"<html><body><h1>Hey!</h1>" +
"<img src='cid:c.png'><h2>Hay!</h2></body></html>")
.embed(attachment().bytes(new File("d:\\c.png")))
.attach(attachment().file("d:\\b.jpg"));
In this example you can see two ways how you can attach your files: embedding them so they appear in HTML content, or common attaching. Of course, you don't have to use fluent interface, its just one option with this library.

Categories