I am writing an application that involves multiple clients. One client uploads a file using FTP to a server, and then another client downloads the file and deletes it. I am using the FTP server kind of as a middleman to exchange information because I do not want the user to have to port forward. I have figured out how to upload a file, but I cannot figure out how to delete the file. The command for deleting a file using FTP is:
DELE <filename>
I have tried doing so, but with no success. Here is the code that I have tried:
public static void deleteFile(String name) throws IOException
{
URL url = new URL("ftp://a1111111:password#mywebsite.com/public_html/misc/screenshots/picture.png;type=i");
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(url.openConnection().getOutputStream()));
writer.write("DELE picture.png");
writer.flush();
}
I have a feeling that the < filename > that I provided may be wrong because I did not include the directory names in the path. However, since I explicitly told the path that of the file in the URL, I am not quite sure what I am supposed to enter for the < filename >.
I have read other questions on this web site about problems very similar to this and all of the responses tell to use a library. The thing is, most of those libraries are written in pure Java so the developers of that library had to figure out a way to do what I am trying to do without a library. I want to do that as well. I do not like attaching extra files besides my own to the things that I make. So please, do not tell me to use a library - it's not what I'm looking for.
If you need any clarification, please ask!
EDIT: If I use this code to receive a response:
byte[] response = new byte[conn.getInputStream().available()];
conn.getInputStream().read(response);
System.out.println("Response: " + new String(response));
My command just gets echoed back:
Response: DELE test1.png
I think you need to do the retrieve and delete in two separate operations; some random documentation I found for FtpURLConnection says, in part:
This class Opens an FTP input (or output) stream given a URL. It works as a one shot FTP transfer :
Login
Get (or Put) the file
Disconnect
I did not see any methods in the documentation that would allow deleting a file.
You may wish to use the URL mechanism to retrieve the file, but I would drop down to using raw sockets to delete the file. Create a new connection to the FTP command port, log in, and issue the DELE command manually. If this is the only step you're taking, you might be able to get away with doing relatively poor error handling and maybe only two read() requests and simply show the output transcript to the user once you're done.
It's a bit dirty, but I completely understand not wanting to carry around a megabyte of additional source to achieve the moral equivalent of echo -e "user foo\npass password\ndele /path/to/file\nlogout" | nc ftp.remote.example.com 21.
Can you use some FTP clients to do the operation?
You can try http://commons.apache.org/net/api-3.1/org/apache/commons/net/ftp/FTPClient.html from the Apache commons-net. It is easy to use.
Related
A text file is located at http://example.com/myText.txt with read and write permissions.
How can I append some line of text on top of it using Java. I see examples with the class OutputStreamWriter but they all use a file as an output. What about http?
(I need the code to be compatible with Java 5)
HTTP in the sense you understand it does not support any write operation of this nature.
There is no clear relationship between a URL and a file on the server; Specifically HTTP GET generally returns you a file-like resource if the URL refers to a web-like server; but the outcomes of HTTP POST are not simply an overwrite of the remote url file they are processed on a case by case basis by the other side.
If this is really what you want I strongly suggest you use FTP which is intended for this purpose.
There is no generic solution to this other than reading the content, appending the line locally, and writing it back with PUT (and that requires that the server supports the PUT method).
I am using ApacheCommons to connect to a server and get information of a specific file. I am looking for a method like FTPFile file = ftpclient.getFile(path); or something like this, so I can give it the path of the file and then be able to get info about that file. By info I mean file.getName() or file.lendth().
The only way I found is using listing methods such as listFiles(path) but it requires using for-loop and so on. But is there any better way or more straight forward?
Use the FTPClient.mlistFile:
public FTPFile mlistFile(String pathname)
Get file details using the MLST command
Of course, this will work only, if your server supports the modern MLST command.
If not, you would have to use a dedicated command for each file property. Like the SIZE for file size (not supported natively by Apache Commons) or the MDTM for file modification time (the FTPClient.getModificationTime).
I am writing a Java server program. Currently I'm using a DataOutputStream to output files to the requesting client or browser. I want the files to be sent to a particular directory on the client's side and not just to the browser (I do not want to write a client program to handle this issue). How do I make the server program write the file to a particular directory on the client's side?
Thanks in advance
Server doesn't know anything about client and neither is this a good idea to allow server to write files or anything on the client directories as it's an Http Security concerns and it won't allow servers to write viruses, changing properties etc on the user's machines who are visiting their web page.
So, what you can do is, client can send a request to server and server will write file on the client (e.g. browser AND not the directory) with sending back some mime-type. There are few mime-types which now a days, browsers can handle and show to the client from within the browser. Client can then download it or discard it as per his/her own choice to any directory on his/her machine.
Here is a bit of code you can write on server side to achieve what I said above:
OutputStream out = response.getOutputStream();
File file = new File(path_to_a_file);
response.setContentType(file_content_type);
response.setHeader("Content-disposition","attachment; filename="+FILE_NAME);
FileInputStream inputStream = new FileInputStream(FILE);
//Read bytes and write until finished
While uploading a image/doc/xlsx file from my AngularJS client to my server-side java using JAX-RS(Jersey) i am getting the following exception,
org.jvnet.mimepull.MIMEParsingException: Reached EOF, but there is no closing MIME boundary.
What is this? Why I am getting this exception? How can I get rid of this?
Note: It works for the files with extension .txt, .html, .yml, .java, .properties
But not working for the for the file with extension .doc, .xlsx, .png, .PNG, .jpeg.. etc.
My Server side code:
#POST
#Path("/{name}")
#Consumes(MediaType.MULTIPART_FORM_DATA)
public String uploadedFiles(#Nonnull #PathParam("name") final String name,
#FormDataParam("file") final InputStream inputStream,
#FormDataParam("file") final FormDataContentDisposition content) {
}
I encountered the same issue. Based on my research, the problem has no relation with the file type. It has a little relation with the size of the uploaded file.
I'm not sure if the root cause is when the uploading file is very big, before the file is uploaded to the server completely, the client disconnects to the server (such as timeout). And I also verified the guess. My test steps is,
1. In client, upload a very big file.
2. Before the get the response from server, which means is uploading file;
close the test client
3. check the server side, you will see the issue.
So To fix it, my solution is add timeout time in client side.
OK, I'm only guessing, but I think I can see a pattern here.
The file types that are working are text based
The file types that are not working are binary
This suggests to me that maybe the problem is that there is some kind of issue with the way that non-text data is being handled by the upload process. Maybe it is being transcoded when it shouldn't be.
Anyway, I suggest that you use some tool like Wireshark to capture the TCP/IP traffic in an upload to see if the upload request body has valid MIME encapsulation.
HI...
Currently I m working in a application in which application allows to access directory (which contains some files) from file server to Application (client).
I tried following code..
URL url=("http://192.168.5.555/file-server/user/images/");
URI uri=url.toURI();
File list[];
list= new File(uri).listFiles();
But its thrown java.lang.IllegalArgumentException Exception.
I don't know how this happen?
I simply access images directory from the given URL (file server).
Help me...
That isn't going to work. The java.io.File operates on the local disk file system only, i.e. on URI's starting with file:// only. It would otherwise indeed going to be too easy to leech files from places where you aren't allowed to do so.
Check if the server in question supports FTP, then you can just use FTPClient#listFiles() for this. If it doesn't, but it supports directory listing, then you need to parse the HTML response containing the directory listing with a HTML parser like Jsoup and then refire a new request on every found link.
If it doesn't support FTP or directory listing, then you're lost and you're probably trying to do bad things.