Download ZIP file from SFTP and save it to local directory - java

I have a mule SFTP connector from which I want to download a ZIP file and store it locally on my computer. I am using only SFTP connector and file outbound.
The issue is that the file is somehow changed to a strange file with .dat extension. I assume this is because of InputSftpStream.
Any ideas how to download a ZIP file and save the same file without any change to computer?

This issue occurs when file name output pattern is not defined. Please update file outbound endpoint like outputPattern="#[message.inboundProperties.originalFilename]" , this will create the file with the same name as the SFTP file name or you can change it to any desired pattern like outputPattern="xyz.zip". Something like
<file:outbound-endpoint path="tmp" outputPattern="#[message.inboundProperties.originalFilename]" connector-ref="File" responseTimeout="10000" doc:name="File"/>
Hope this help.

As a small workaround, you could change the file extension after the fact with:
File.renameTo(File dest)

Related

How can I modify files at server by servlet?

I need to modify html file that is placed at server folder from my servlet.
No other way than read it by FileInputStream to byte[], convert to String[] splitting lines by "\n", change what I need and then rewrite it.
I don't see.
This is not possible by design. Your server might just have to serve a .WAR file. If the server is not configured to unzip it, your server will have to read all files directly from this archive. You can now guess that you cannot write at this location.
You would need to create some kind of working directory and also serve files from there, too. You can always use this directory as working directory:
File workingDir = (File)servletContext.getAttribute(ServletContext.TEMPDIR);

Relative path in 'file' protocol for URL

I have an implementation which reads a file and uses java.net.URL to take a path to open connection to.
In production code, the link of file will be on FTP but for testing the logic I want to use a file locally which I can read using file protocol instead of ftp.
I am using Spring and changed the file path to something like below-
application-test.properties
#file location
enzyme.dat.ftp.link=file:///Users/username/project/src/test-integration/resources/input.dat
As you can see its a absolute path, which I need to make relative to my project. How can I do it?
I just need something like below-
#file location
enzyme.dat.ftp.link=file://${project.basedir}/src/test-integration/resources/input.dat
new URL("file:relative/path/to/file.txt")
The path is calculated starting from the current working dir of the application.
This answers the exact question, but actually, the recommendation to read the resource from classpath is better unless the file is really external to the project.
If the file you want to read is on the classpath then you could use something like the following:
new ClassPathResource("input.dat").getURL();
when trying to read the properties file, that way you can simply reference the file name ("input.dat") in your test properties. If the file is not on the classpath then you'll have to use a different mechanism.

Apache Camel File Formats

I am working on a project that automatically uploads files to the SFTP server. My project is now in production and functioning as per the initial requirement spec. My project picks-up a txt file from a predefined location. Now because of the size limit on the SFTP server, the txt file must now be Zipped before the upload.
I now want to change my camel route to cater not only for txt files but also for xls, csv and Zip files.
How do i do that?
Currently my route looks as follows:
from("quartz://myscheduler?cron={{cron}}")
.pollEnrich("file:{{pickuplocation}}?moveFailed=error/${file:name.noext}_${date:now:yyyyMMddHHmmssSSS}.${file:ext}&move=SFTPCompleted/${date:now:MMM}-${date:now:yyyy}/${file:name.noext}_${date:now:yyyyMMddHHmmssSSS}.${file:ext}")
.setHeader("CamelFileName", simple("${file:name}"))
.setHeader("RouteID",constant("Route ID"))
.multicast()
.to("sftp://"+username+"#"+SftpLocation+"password="+password+"&stepwise=false&disconnect=true&fileName=${file:name.noext}.txt")
.end()
TIA
I don't see why you use such a complicated way of copying files. If I'm not mistaken you could just use this:
from("file:{{pickuplocation}}?moveFailed=error/${file:name.noext}_${date:now:yyyyMMddHHmmssSSS}.${file:ext}&move=SFTPCompleted/${date:now:MMM}-${date:now:yyyy}/${file:name.noext}_${date:now:yyyyMMddHHmmssSSS}.${file:ext})
.to("sftp://"+username+"#"+SftpLocation+"password="+password+"&stepwise=false&disconnect=true")
This will pickup any file in the pickupLocation directory and the FTP compenent will write the file with the same name to the remote server.
You can use the 'include' parameter of the File component to set a regex to match only certain file types: http://camel.apache.org/file2.html

Upload file with the given destination path in java

In a java web application (struts) runs on tomcat, I need to upload a file (of any type) to a destination folder which will be taken as input parameter from the user. The destination can not only within the server directory but also anywhere in the system. Is there any api available for file upload or can this be acheived using java IO? Any suggestions with sample code will be appreciated.
Thanks.
Try changing "user.dir" property which has path of server directory. Here is sample code may be of some help
System.setProperty("user.dir", <destination path on your system>);
File f = new File(System.getProperty("user.dir"));
Use common-fileupload. There are more example for that here.

Jsch renaming file fails

I am uploading a large file by SFTP over Jsch. During the upload process, the old file should be available, so I'm uploading to a temp file and rename it to the new file.
final String tmpName = dest + "_tmp";
channel.put(source, tmpName);
channel.rename(tmpName, dest);
The upload is ok but the renaming fails:
ERROR: Failed to upload files
4: Failure
at com.jcraft.jsch.ChannelSftp.throwStatusError(ChannelSftp.java:2491)
at com.jcraft.jsch.ChannelSftp.rename(ChannelSftp.java:1665)
...
I can't figure out where the problem is. Please help
The target file already exists. Try deleting the existing file before renaming.
I have tried rename and it worked fine for me. there was another file with same and i tried to rename new file to existing one. and it worked.
so no need to check file exist or not if you want to overwrite.

Categories