Upload ByteArrayInputStream to FTP in Java - java

I am using Apache common net for uploading a file to FTP server. The problem is: I have a string to write in CSV file and I want to upload it to FTP server. I dont want to store the file on local, so I do as following:
FTPClient ftpclient = new FTPClient();
try {
String csvContent = ".......";
InputStream is = new ByteArrayInputStream(csvContent .getBytes());
ftpclient.connect(ftpServer);
ftpclient.login(user, password);
ftpclient.setFileType(FTP.BINARY_FILE_TYPE);
ftpclient.changeWorkingDirectory(directory);
ftpclient.storeFile("test.csv", is);
System.out.println(ftpclient.getReplyCode());
ftpclient.logout();
}
The method ftpclient.getReplyCode() always returns error 550 "test.csv": No such file or directory.
Could you help me to fix this? Thanks

Related

how download file by url in jsoup

i have a website to download excel file. and now i need to send parameters to download file with this site url by jsoup. when i get bodystream(), i get a error,i do not know why and how can i solute this matter.
Connection con = Jsoup.connect(url);
File downloadFile = File.createTempFile("TMP", ".xlsx");
con=con.timeout(300000);
con = con.header("Connection", "keep-alive")
.header("Cache-Control", "max-age=0");
con=con.data(parameters);
con=con.cookies(cookie);
Connection.Response res = con.ignoreContentType(true).method(POST).execute();
FileUtils.copyInputStreamToFile(res.bodyStream(), downloadFile);
but i got java.lang.IllegalArgumentException: Request has already been read
※sometimes i download download successfully with same code and parameters.
can you tell me how to solute this matter and download file by this way?
The following worked for me (with some changes to specify a URL; but you can include your other changes such as setting the cookies and to POST).
This just uses the inbuilt Java helper utility to read the input stream and save it to a file.
Given the error message you mentioned, I wonder if the FileUtils method you're using (what dependency is that from?) is sometimes re-reading the file.
String url = "https://jsoup.org/rez/html5-logo.svg";
File downloadFile = File.createTempFile("TMP", ".svg");
Connection con = Jsoup.connect(url)
.timeout(300000)
.header("Cache-Control", "max-age=0")
.ignoreContentType(true);
Connection.Response res = con.execute();
BufferedInputStream body = res.bodyStream();
Files.copy(body, downloadFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
System.out.println("Saved URL to " + downloadFile.getAbsolutePath());
Alternatively, if you still get the same error, you could try reading the whole body into a byte array before saving:
Connection.Response res = con.execute();
byte[] bytes = res.bodyAsBytes();
Files.write(downloadFile.toPath(), bytes);

Why my HTTPS file download corrupts .zip files?

I'm trying to download zip files from internet using following code:
public void getFile(String updateURL) throws Exception {
URL url = new URL(updateURL);
HttpURLConnection httpsConn = (HttpURLConnection) url.openConnection();
httpsConn.setRequestMethod("GET");
TrustModifier.relaxHostChecking(httpsConn);
int responseCode = httpsConn.getResponseCode();
if (responseCode == HttpsURLConnection.HTTP_OK) {
String fileName = "fileFromNet";
try (FileOutputStream outputStream = new FileOutputStream(fileName)) {
ReadableByteChannel rbc = Channels.newChannel(httpsConn.getInputStream());
outputStream.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
}
}
httpsConn.disconnect();
}
TrustModifier is a class used to solve the "trust issue": http://www.obsidianscheduler.com/blog/ignoring-self-signed-certificates-in-java/
The code above works well for zip files available via plain http or for non compressed files exposed via https but but if I try to download a zip file exposed via https endpoint only a small fragment of original file will be downloaded. I have tested with different download links from internet and always got the same result.
Does anybody has an idea what I've been doing wrong here?
Thank you.
transferFrom() must be called in a loop until the transfer is complete, and in this case the only way you can know that is by adding up the return values of transferFrom() until they equal the Content-length of the HTTP response.
Actually the problem was in the TrustModifier Class I was using to switch off the servier certificate check. Once I removed it because I didn't need it any longer (I took the certificate from server and put it in a local trust store), my problem was solved.

writing to a file kept in ftp server

I have a requirement in which I have to write to files kept in FTP server using java ,I cant write to a file in the local server and then transfer it to ftp due to sensitivity of the data,can anyone share some thoughts/links on this.
Any Help will be greatly appreciated.
Apology for not posting my code snippet earlier below is the code I wrote
Student stu=new Student();
stu.setName("xyz");
stu.setRoll("12");
ftpClient.changeWorkingDirectory("/mydirectory/release/");
//abc.txt is the file on the server
FileOutputStream fos=(FileOutputStream)ftpClient.appendFileStream("abc.txt");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(stu);
Iam not getting any exception ,but also not able to write into the file..
yes I want to upload bytes via ftp from memory..
Thanks
I got the solution,I had to use url instead of ftpClient
URL url = new URL("ftp://user:pass#myftp.abc.com/myFile.txt;type=i");
URLConnection urlc = url.openConnection();
OutputStream os = urlc.getOutputStream(); // To upload
OutputStream buffer = new BufferedOutputStream(os);
ObjectOutput output = new ObjectOutputStream(buffer);
output.writeObject(myObject);
buffer.close();
os.close();
output.close();
It looks like Apache's FTPClient class, documented here, will do what you want.

FTP file upload failure Java

I use Apache's FTPClient and FTPServer libraries in my Java project. Server and client are on the same machine.
My FTPServer is supposed to be a local server,nothing related to the Internet. I can connect to the FTPServer from the client(I get 230 as reply code) but i cant seem to do anything. I cant store or retrieve any files.
I read almost every question related to this matter but people who asked other questions were be able to send simple files and had trouble with sending files like pdf etc. I just need to send or retrieve text files.
Any suggestions?
FTPClient client = new FTPClient();
String host = "mypc";
String Name = "user";
String Pass = "12345";
client.connect(host);
client.login(Name,Pass);
System.out.println("Reply Code: " +client.getReplyCode());
File file = new File("C:\\.....myfile..txt");
FileInputStream in = new FileInputStream("C:\\.....myfile..txt");
boolean isStored = client.storeFile("uploadedfile.txt", in);
in.close();
client.logout();
System.out.println("isStored: " +isStored);
I didnt put the real path names. It returns false,no exceptions etc. This might be because of they're on the same machine?
Edit: Turned out i needed write permission to send a file to ftpserver. By default, it doesnt give users write permission. How can i give users write permission using Apache's ftpserver library?
Problem Solved:
This is how to give a user write permission. I added this snippet to server side and it worked.
List<Authority> auths = new ArrayList<Authority>();
Authority auth = new WritePermission();
auths.add(auth);
user.setAuthorities(auths);
There's term Authority written in this symbol -> < > after List and ArrayList in the first line. Site doesn't see words in <> symbol.

Calling webservice via server causes java.net.MalformedURLException: no protocol

I am writing a web-service, which parses an xml file. In the client, I read the whole content of the xml into a String then I give it to the web-service.
If I run my web-service with main as a Java-Application (for tests) there is no problem, no error messages. However when I try to call it via the server, I get the following error:
java.net.MalformedURLException: no protocol
I use the same xml file, the same code (without main), and I just cannot figure out, what the cause of the error can be.
here is my code:
DOMParser parser=new DOMParser();
try {
parser.setFeature("http://xml.org/sax/features/validation", true);
parser.setFeature("http://apache.org/xml/features/validation/schema",true);
parser.setFeature("http://apache.org/xml/features/validation/dynamic",true);
parser.setErrorHandler(new myErrorHandler());
parser.parse(new InputSource(new StringReader(xmlFile)));
document=parser.getDocument();
xmlFile is constructed in the client so:
String myFile ="C:/test.xml";
File file=new File(myFile);
String myString="";
FileInputStream fis=new FileInputStream(file);
BufferedInputStream bis=new BufferedInputStream(fis);
DataInputStream dis=new DataInputStream(bis);
while (dis.available()!=0) {
myString=myString+dis.readLine();
}
fis.close();
bis.close();
dis.close();
Any suggestions will be appreciated!
Add the protocol (http) to your xmlns:
<user xmlns:xsi="http://w3.org...etc"

Categories

Resources