Jsch renaming file fails - java

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.

Related

Download ZIP file from SFTP and save it to local directory

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)

Storing files in project directory using Spring

I am trying to work through Spring tutorials on file uploads. What I'm trying to do is have the file be saved to a folder within the project. The folder is called "files" and is separate from the src folder.
|bin
|build
|src
|files
I have this code which accepts a file upload:
public #ResponseBody String handleFileUpload(#RequestParam("name") String name,
#RequestParam("file") MultipartFile file){
if (!file.isEmpty()) {
try {
byte[] bytes = file.getBytes();
BufferedOutputStream stream =
new BufferedOutputStream(new FileOutputStream(new File(name)));
stream.write(bytes);
stream.close();
//file.transferTo(); help?
return "You successfully uploaded " + name + "!";
What I want to do is use transferTo()to move the file to the "files" directory. When I try the true path, or try some sort of relative path I get this error which is created in the web window I am uploading files in.
Failed to upload file => "uploadedFileName/directory" does not exist
I am not sure why the file name is being appended to the directory path. Any assistance on this is much appreciated.
When you are trying to save the uploaded file to the file "uploadedFileName/directory", you are using relative file path. It's relative to the current working directory of the java process (java process, running your Tomcat Application Server or whatever appserver you are using). And that current working directory is not your project root. The following code:
System.out.println(new File(name).getAbsolutePath())
will print you the exactly path where you are trying to save your uploaded file.
To fix that issue you have to explicitly specify your project root:
File rootDir = new File("C:/Projects/myTestProject");
File uploadedFile = new File(rootDir, name);
file.transferTo(uploadedFile);
In real project you will want not to hardcode that rootDir path, but to retrieve it from some configuration file.
Don't rely to the current working directory of the application server. It could point to any directory.
PS that's out of scope of this question, but please be careful with saving data to the user-provided file names. Malicious user could post file with name "../../../../../../../../SomeSensitiveDirectory/SomeFile" and will overwrite that file unless you explicitly check that input parameter name for bad characters.
When you upload a file, you have to save it to another file.
You should use File#createTempFile() which takes a directory instead.
<p style="border-style:solid; border-color:#FFFFFF;">
<br>
File file = File.createTempFile("upload-", ".bin", new File("/path/to/your/uploads"));
<br><br>
item.write(file);
<br><br></p>

Write in a txt file located in /raw folder

here's my problem.
I have a txt file in the /raw folder and I want to write into it.
From the Android guide i saw this:
Tip: If you want to save a static file in your application at compile time, save the file in your project res/raw/ directory. You can open it with openRawResource(), passing the R.raw. resource ID. This method returns an InputStream that you can use to read the file (but you cannot write to the original file).
So in my code I did this:
FileOutputStream myFile = openRawResource(R.raw.max_easy, Context.MODE_PRIVATE);
But gives me error in openRawResource()... How do i solve this?
Thank you for the help!
I have a txt file in the /raw folder and I want to write into it
That is not possible at runtime. Resources and assets are read-only at runtime.
But gives me error in openRawResource()... How do i solve this?
openRawResource() is for reading in the resource, and it gives you an InputStream. You are welcome to write your data to an ordinary file, such as on internal storage.

Can't find folder or file created with Android App with windows file exlorer

I'm creating a directory and a text file on the sdcard in one of my apps because I want to be able to move it to my computer for analysis. But I can't find the folder or the file I'm creating on my sdcard using the file browser on my computer.
I CAN find and read the file using my phones file manager but not using the file browser in windows.
So the file and folder are succesfully created and I can write to the file, I can also find and read the file using the file manager on my phone but I can't find either directory or file using my computer.
I have a uses permission for the application to allow it to write to external storage.
This is the code I use to create the file and directory.
String fileName = "testFil.txt";
String path = Environment.getExternalStorageDirectory().getAbsolutePath()+"/PulsApp";
File appDirectory = new File(path);
appDirectory.mkdirs();
File file = new File(path, fileName);
try {
file.createNewFile();
} catch (IOException e) {
}
Does anyone know what the problem is and how to fix it? I really need to be able to write files to my sdcard so I can transfer them to my computer.
I am completely baffled by this problem since all the research I've done point to that everyone else is doing the same thing.
If your device is running Android 3.0 or higher, you also need to use MediaScannerConnection to index your newly-created file before it will show up on a development PC's file explorer.
More accurately, the newly-created file needs to be indexed by the MediaStore. That will eventually happen for other reasons (e.g., device reboot). However, you are better served using scanFile() on MediaScannerConnection to get it to happen more quickly.
I blogged about this last summer.
Sometimes that the MediaScannerConnection will recognize the folder as a unknown type file, so try to create another folder inside the original one can avoid this problem.
I have met the same problem, and I use the method in the comment
And it works for me.

Copy and re-name a file on android

I have a file which I am using as a temporary file until the user selects to save this file, How do I go about making a copy of this file but at the same time re-naming it. Example below:
File 1:
/sdcard/Folder/file1.wav
File 2:
Copy of file 1, renamed to file2.wav in /sdcard/Folder/file2.wav
I understand using Environment.getExternalStorageDirectory().getAbsolutePath() gets me the path to the external storage so i'm not asking how I get to the sdcard or whatever. just to make a copy of a file and re-name it.
Try Apache Commons IO FileUtils.moveFile(File, File).

Categories