Smart file upload and download is very easy in comparison of gwt and gwt-ext.
Client side
Canvas canvas = new Canvas();
canvas.setSize("355px", "280px");
final DynamicForm uploadForm = new DynamicForm();
uploadForm.setEncoding(Encoding.MULTIPART);
UploadItem fileItem = new UploadItem("image");
TextItem nameItem = new TextItem("imageName");
TextItem descriptionItem = new TextItem("description");
HiddenItem spaceImageIdItem = new HiddenItem("spaceImageId");
HiddenItem propertyIdItem = new HiddenItem("propertyId");
propertyIdItem.setValue(23);
spaceImageIdItem.setValue(0);
canvas.addChild(uploadForm);
uploadForm.moveTo(6, 109);
uploadForm.setAction("/XmlServlet1");
IButton uploadButton = new IButton("Download");
canvas.addChild(uploadButton);
uploadButton.moveTo(218, 156);
uploadButton.addClickHandler(new com.smartgwt.client.widgets.events.ClickHandler(){
@Override
public void onClick(ClickEvent event) {
uploadForm.submitForm();
}
});
layout.addMember(canvas);
Server side code same as gwt ext xml servlet .java file.
xmlServlet1.java
package com.server;
import java.io.*;
import java.sql.*;
import java.util.*;
import java.text.*;
import java.util.regex.*;
import org.mortbay.jetty.Response;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.sql.*;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletInputStream.*;
import java.io.PrintWriter;
public class XmlServlet1 extends HttpServlet {
public void doPost(HttpServletRequest req,HttpServletResponse res)
{
File uploadedFile;
System.out.print("on server");
try{
// String filename =
File uploadedFile1=new File("./img/"+"happybir.jpg" );
String kk=uploadedFile1.getAbsolutePath();
System.out.print(kk);
File f=new File(kk);
//System.out.println("foder is " + folder);
res.setContentType("application/octet-stream");
res.setHeader("Content-disposition", "attachment;filename=\"" +"happybir.jpg" + "\"");
//res.setHeader("Content-Disposition","attachment; filename=;");
ServletOutputStream stream = res.getOutputStream();
BufferedInputStream fif = new BufferedInputStream(new FileInputStream(f));
int data;
while((data = fif.read()) != -1) {
stream.write(data);
}
fif.close();
stream.close();
}catch(Exception e)
{
System.out.println("Exception Due to"+e);
e.printStackTrace();
}
}
}
No comments:
Post a Comment