writing to a file kept in ftp server - java

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.

Related

Java web start applet cannot connect with localhost servlet

I can't find problem similar to my problem with jws so I write here.
I hava java applet that I try to run with jws technology. In applet I have method that send a object to servlet and try to getInputStream. Unfortunetly I have a exception:
java.io.StreamCorruptedException: invalid stream header: 3C21444F
at java.io.ObjectInputStream.readStreamHeader
at java.io.ObjectInputStream.
method example:
String url = "http://localhost/servlet/myServlet";
URL servletUrl = new URL(url);
URLConnection urlConn = servletUrl.openConnection();
urlConn.setDoOutput(true);
urlConn.setDoInput(true);
urlConn.setUseCaches(false);
urlConn.setRequestProperty("Content-Type", "application/x-java-serialized-object");
ObjectOutputStream oos = new ObjectOutputStream(urlConn.getOutputStream());
oos.writeObject(myobject);
oos.close();
ObjectInputStream ois = new ObjectInputStream(urlConn.getInputStream()); //StreamCorruptedException
Object obj = ois.readObject();
oIS.close();
I have no ide why. Please type your ideas in post's.
from oracle's forum:
An object serialization stream should not start with 3C21444F, which
is ASCII for
<!DO
This means that the server/servlet, for some
reason, does not send you what you think it should. It's rather the
beginning of an XML document, perhaps an error page.
It was due the servlet authorization system .

Upload ByteArrayInputStream to FTP in 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

java - Download and then write image as servlet response

How to download an image from a server and then write it as a response in my servlet.
What is the best way to do it keeping good performance?
Here's my code:
JSONObject imageJson;
... //getting my JSON
String imgUrl = imageJson.get("img");
if you don't need to hide your image source and if server is accessible from the client as well, I'd just point your response to remote server (as you already have the url) => you don't need to do a download to your server first, but possibly client could access it directly => you don't waste your resources.
However if you still need to download it to your server first, following post might help: Writing image to servlet response with best performance
It's important to avoid intermediate buffering of image in servlet. Instead just stream whatever was received to the servlet response:
InputStream is = new URL(imgUrl).openStream();
OutputStream os = servletResponse.getOutputStream();
IOUtils.copy(is, os);
is.close();
I'm using IOUtils from Apache Commons (not necessary, but useful).
The complete solution : download a map and save to file.
String imgUrl = "http://maps.googleapis.com/maps/api/staticmap?center=-15.800513,-47.91378&zoom=11&size=200x200&sensor=false";
InputStream is = new URL(imgUrl).openStream();
File archivo = new File("c://temp//mapa.png");
archivo.setWritable(true);
OutputStream output = new FileOutputStream(archivo);
IOUtils.copy(is, output);
IOUtils.closeQuietly(output);
is.close();

reading bytes from web site

I am trying to create a proxy server.
I want to read the websites byte by byte so that I can display images and all other stuff. I tried readLine but I can't display images. Do you have any suggestions how I can change my code and send all data with DataOutputStream object to browser ?
try{
Socket s = new Socket(InetAddress.getByName(req.hostname), 80);
String file = parcala(req.url);
DataOutputStream out = new DataOutputStream(clientSocket.getOutputStream());
BufferedReader dis = new BufferedReader(new InputStreamReader(s.getInputStream()));
PrintWriter socketOut = new PrintWriter(s.getOutputStream());
socketOut.print("GET "+ req.url + "\n\n");
//socketOut.print("Host: "+req.hostname);
socketOut.flush();
String line;
while ((line = dis.readLine()) != null){
System.out.println(line);
}
}
catch (Exception e){}
}
Edited Part
This is what I should have to do. I can block banned web sites but can't allow other web sites in my program.
In the filter program, you will open a TCP socket at the specified port and wait for connections. If a
request comes (i.e. the client types a URL to access a web site), the application will process it to
decide whether access is allowed or not and then, using the same socket, it will send the reply back
to the client. After the client opened her connection to WebPolice (and her request has been checked
and is allowed), the real web page needs to be shown to the client. Therefore, since the user already gave her request, now it is WebPolice’s turn to forward the request so that the user can get the web page. Thus, WebPolice acts as a client and requests the web page. This means you need to open a connection to the web server (without closing the connection to the user), forward the request over this connection, get the reply and forward it back to the client. You will use threads to handle multiple connections (at the same time and/or at different times).
I don't know what exactly you're trying to do, but crafting an HTTP request and reading its response incorporates somewhat more than you have done here. Readline won't work on binary data anyway.
You can take a look at the URLConnection class (stolen here):
URL oracle = new URL("http://www.oracle.com/");
URLConnection yc = oracle.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()));
Then you can read textual or binary data from the in object.
Read line will treat the line read as a String, so unless you want to mess around with conversions over to bytes, I wouldn't recommend that.
I would just read bytes until you can't read anymore, then write them out to a file, this should allow you to grab the images, keeping file headers intact which can be important when dealing with files other than text.
Hope this helps.
Instead of using BufferedReader you can try to use InputStream.
It has several methods for reading bytes.
http://docs.oracle.com/javase/6/docs/api/java/io/InputStream.html

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