package com.server;
import java.io.*;
import java.sql.*;
import java.util.*;
import java.text.*;
import java.util.regex.*;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.*;
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 XmlServlet 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();
}
}
}
Client side
package com.client;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.RootPanel;
import com.gwtext.client.widgets.Window;
public class FileDownload implements EntryPoint {
public void onModuleLoad() {
RootPanel rootPanel = RootPanel.get();
Button clickMeButton = new Button();
rootPanel.add(clickMeButton);
clickMeButton.setText("Click me!");
clickMeButton.addClickHandler(new ClickHandler(){
public void onClick(ClickEvent event) {
FileUploadWindow window = new FileUploadWindow();
window.setVisible(true);
}
});
}
}
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.RootPanel;
import com.gwtext.client.widgets.Window;
public class FileDownload implements EntryPoint {
public void onModuleLoad() {
RootPanel rootPanel = RootPanel.get();
Button clickMeButton = new Button();
rootPanel.add(clickMeButton);
clickMeButton.setText("Click me!");
clickMeButton.addClickHandler(new ClickHandler(){
public void onClick(ClickEvent event) {
FileUploadWindow window = new FileUploadWindow();
window.setVisible(true);
}
});
}
}
FileUploadWindow.java
package com.client;
import com.google.gwt.core.client.GWT;
public class FileUploadWindow {
FormPanel formPanel = new FormPanel();
public FileUploadWindow() {
RootPanel r=RootPanel.get();
formPanel.setFileUpload(true);
//setup error reader to process from submit response from server
RecordDef errorRecordDef = new RecordDef(new FieldDef[]{
new StringFieldDef("id"),
new StringFieldDef("msg")
});
XmlReader errorReader = new XmlReader("field", errorRecordDef);
errorReader.setSuccess("@success");
formPanel.setErrorReader(errorReader);
//final TextField textField = new TextField("Photo", "file");
//textField.setInputType("file");
//textField.setSize("334px", "28px");
//formPanel.add(textField);
formPanel.add(new Button("Submit",new ButtonListenerAdapter() {
public void onClick(Button button, EventObject e) {
MessageBox.confirm("Confirm", "Do you want to submit?",
new MessageBox.ConfirmCallback() {
public void execute(String btnID) {
if (btnID.equals("yes")) {
formPanel.getForm().submit(GWT.getModuleBaseURL()+"XmlServlet", null, Connection.GET, "Saving Data...", false);
}
}
});
}
}));
formPanel.addFormListener(new FormListenerAdapter(){
public boolean doBeforeAction(Form form) {return true;}
public void onActionComplete(Form form, int httpStatus, java.lang.String responseText){
System.out.println(responseText);
Image image = new Image("./img/"+"monica.txt");
image.setSize("300px", "300px");
}
public void onActionFailed(Form form, int httpStatus, java.lang.String responseText){
com.google.gwt.user.client.Window.alert("File upload is failed.");
}
});
r.add(formPanel);
formPanel.setSize("100px","100px");
}
}
import com.google.gwt.core.client.GWT;
public class FileUploadWindow {
FormPanel formPanel = new FormPanel();
public FileUploadWindow() {
RootPanel r=RootPanel.get();
formPanel.setFileUpload(true);
//setup error reader to process from submit response from server
RecordDef errorRecordDef = new RecordDef(new FieldDef[]{
new StringFieldDef("id"),
new StringFieldDef("msg")
});
XmlReader errorReader = new XmlReader("field", errorRecordDef);
errorReader.setSuccess("@success");
formPanel.setErrorReader(errorReader);
//final TextField textField = new TextField("Photo", "file");
//textField.setInputType("file");
//textField.setSize("334px", "28px");
//formPanel.add(textField);
formPanel.add(new Button("Submit",new ButtonListenerAdapter() {
public void onClick(Button button, EventObject e) {
MessageBox.confirm("Confirm", "Do you want to submit?",
new MessageBox.ConfirmCallback() {
public void execute(String btnID) {
if (btnID.equals("yes")) {
formPanel.getForm().submit(GWT.getModuleBaseURL()+"XmlServlet", null, Connection.GET, "Saving Data...", false);
}
}
});
}
}));
formPanel.addFormListener(new FormListenerAdapter(){
public boolean doBeforeAction(Form form) {return true;}
public void onActionComplete(Form form, int httpStatus, java.lang.String responseText){
System.out.println(responseText);
Image image = new Image("./img/"+"monica.txt");
image.setSize("300px", "300px");
}
public void onActionFailed(Form form, int httpStatus, java.lang.String responseText){
com.google.gwt.user.client.Window.alert("File upload is failed.");
}
});
r.add(formPanel);
formPanel.setSize("100px","100px");
}
}
No comments:
Post a Comment