• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

save file to remote server from an applet

 
Miral Makadia
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I have problem with client server communication with an appplet,

I have open pdf file from server using applet now i make an annotation on it and i want to save that updated file on server using applet.

I have tried with URL connection,

String strURL ="http://127.0.0.1:8080/test/TestDataHandler";
URL serverUrl = new URL(strURL);
System.out.println("Save.action method call 4");
URLConnection dataHandlerConnection = serverUrl.openConnection();


If i am request from browser for that url "http://127.0.0.1:8080/test/TestDataHandler", its working nice . but when i try form applet than it is not work,


can any one help me.

Thanks in advance.
 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please read ItDoesntWorkIsUseless and TellTheDetails: how are you trying to store the file, and what exactly happens? Note that for uploading a file over HTTP you need a server-side component (like a servlet) that can handle file uploads - do you have that in place? If not, start here: http://faq.javaranch.com/java/FileUpload
 
Miral Makadia
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to save pdf file to server from applet and for that i make one sevlet and request form applet , but request is not goes to perticular servlet from applet.and execute completly without any error

I have one sevlet TestDataHandler sevlet and

i called that sevlet from URL class, but request is not goes to from applet
code is here

applet code
public void action(final ViewerEvent event) {
try{
System.out.println("Save.action method call 1");
final PDF pdf = event.getPDF();
//final Exporter[] chosenexporter = new Exporter[1];
Exporter ex = new PDFExporter();
System.out.println("Save.action method call 2");
final DocumentPanel docpanel = event.getDocumentPanel();
final PDFViewer viewer = event.getViewer();
final JComponent secondarycomp = null;
System.out.println("Save.action method call 3");

String strURL ="http://127.0.0.1:8080/test/TestDataHandler"; //
URL serverUrl = new URL(strURL);
//System.out.println("serverurl..."+serverUrl.getFile().getBytes());
System.out.println("Save.action method call 4");
URLConnection dataHandlerConnection = serverUrl.openConnection();


System.out.println("Save.action method call 5");
dataHandlerConnection.setDoOutput(true);
System.out.println("Save.action method call 6");
// dataHandlerConnection.connect();
OutputStream out = dataHandlerConnection.getOutputStream();
DataOutputStream dos = new DataOutputStream(out);
//dos.write("This is test input".getBytes());
dos.writeBytes("This is test input");
//out.write("This is test input".getBytes());
dos.flush();
dos.close();
//System.out.println("Exporter is : " + chosenexporter[0]);
// System.out.println("docpanel is : " + docpanel);
// Exporter.ExporterTask task = ex.getExporter(docpanel, docpanel.getPDF(), secondarycomp, out);
// System.out.println("Save.action method call 7");
// task.start(viewer, Util.getUIString("PDFViewer.SavingFile", "test file"));
System.out.println("Save.action method call 8");
}catch (Exception e) {
System.out.println("Exception while shaving data in Save.action()");
e.printStackTrace();
}
}



sevlet code



public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
try{
System.out.println("Do GET method is called");
resp.setContentType("text/html");
InputStream is = req.getInputStream();
byte[] b = new byte[1024];
while(is.read(b)!=-1){
System.out.println("read byte :" + b.toString());
}


String contentType = req.getContentType();
System.out.println("Content type is :: " +contentType);
DataInputStream in = new DataInputStream(req.getInputStream());
int formDataLength = req.getContentLength();
byte dataBytes[] = new byte[formDataLength];
int byteRead = 0;
int totalBytesRead = 0;
while (totalBytesRead < formDataLength) {
byteRead = in.read(dataBytes, totalBytesRead, formDataLength);
totalBytesRead += byteRead;
}
System.out.println("read byte :" + dataBytes.toString());
System.out.println("Do GET method is over");
//resp.getWriter().println("DataHandlerServlet doGet is called");
}catch (Exception e) {
e.printStackTrace();
}
}
 
Swastik Dey
Rancher
Posts: 1602
5
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please use code tags to post codes for better readability. By the way do you get any exception at either end, and why are you performing the read action twice in servlet?
 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Calling URLConnection.setDoOutput(true) will cause the request to be a POST (instead of a GET), so that's what the servlet should expect.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic