在GWT中上载裁剪的图像
我正在开发一个web应用程序,用户在其中选择一个图像,裁剪它,最后将其上传到服务器。因此,通过FileUpload小部件,我允许用户选择图像源,获取其路径并使用构造函数在GWT中上载裁剪的图像,gwt,Gwt,我正在开发一个web应用程序,用户在其中选择一个图像,裁剪它,最后将其上传到服务器。因此,通过FileUpload小部件,我允许用户选择图像源,获取其路径并使用构造函数 Image(java.lang.String url, int left, int top, int width, int height); 我得到了裁剪过的图像对象 但现在,我不知道如何上传到服务器上的形象。有人知道解决方案吗?您可以找到一个关于如何将文件上载到服务器的好例子 编辑 您需要做的是将图像上传到服务器,在客户端检
Image(java.lang.String url, int left, int top, int width, int height);
我得到了裁剪过的图像对象
但现在,我不知道如何上传到服务器上的形象。有人知道解决方案吗?您可以找到一个关于如何将文件上载到服务器的好例子 编辑 您需要做的是将图像上传到服务器,在客户端检索图像,在客户端进行可视化裁剪,将裁剪参数发送到服务器,最后在服务器上进行实际裁剪。从上述项目开始,我就是这样做的:
vPanel.add(new Button("Submit", new ClickHandler() {
public void onClick(ClickEvent event) {
form.submit();
}
}));
用户选择图像后,我们将其与文件上载一起上载,并在服务器中将其保存在一个目录中:
List<FileItem> items = fileUpload.parseRequest(request);
Iterator<FileItem> iter = items.iterator();
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
File file = new File(<images-path>,fileName);
Streams.copy(item.getInputStream(),new FileOutputStream(file), true);
}
现在,我们必须添加一个裁剪服务,使实际裁剪能够在服务器中发生。我使用RCP服务来实现这一点:
public class CropServiceImpl extends RemoteServiceServlet implements CropService {
public Boolean crop(String name, int x, int y, int width, int height)
{
try
{
BufferedImage outImage = ImageIO.read(new File("<images-path>"+name));
BufferedImage cropped = outImage.getSubimage(x, y, width, height);
ImageIO.write(cropped, "jpg", new File("<images-path>","cropped"+name));
return true;
}
catch (IOException e)
{
e.printStackTrace();
}
return false;
}
}
最后,回到客户端,我们使用从裁剪中获得的参数调用按钮操作中的方法:
vPanel.add(new Button("Crop", new ClickHandler()
{
public void onClick(ClickEvent event)
{
cropService.crop(getName(), (int) crop.getSelectionXCoordinate(),
(int) crop.getSelectionYCoordinate(), (int) crop.getSelectionWidth(),
(int) crop.getSelectionHeight(), new AsyncCallback<Boolean>()
{
public void onFailure(Throwable arg0)
{
// something went wrong with the call
}
public void onSuccess(Boolean arg0)
{
if (arg0)
{
// the cropped file now lives in the server
}
else
{
// an error happened in the server
}
}
});
}
}));
好了,很抱歉发了这么长的帖子,希望能有所帮助。你可以找到一个关于如何将文件上传到服务器的好例子 编辑 您需要做的是将图像上传到服务器,在客户端检索图像,在客户端进行可视化裁剪,将裁剪参数发送到服务器,最后在服务器上进行实际裁剪。从上述项目开始,我就是这样做的:
vPanel.add(new Button("Submit", new ClickHandler() {
public void onClick(ClickEvent event) {
form.submit();
}
}));
用户选择图像后,我们将其与文件上载一起上载,并在服务器中将其保存在一个目录中:
List<FileItem> items = fileUpload.parseRequest(request);
Iterator<FileItem> iter = items.iterator();
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
File file = new File(<images-path>,fileName);
Streams.copy(item.getInputStream(),new FileOutputStream(file), true);
}
现在,我们必须添加一个裁剪服务,使实际裁剪能够在服务器中发生。我使用RCP服务来实现这一点:
public class CropServiceImpl extends RemoteServiceServlet implements CropService {
public Boolean crop(String name, int x, int y, int width, int height)
{
try
{
BufferedImage outImage = ImageIO.read(new File("<images-path>"+name));
BufferedImage cropped = outImage.getSubimage(x, y, width, height);
ImageIO.write(cropped, "jpg", new File("<images-path>","cropped"+name));
return true;
}
catch (IOException e)
{
e.printStackTrace();
}
return false;
}
}
最后,回到客户端,我们使用从裁剪中获得的参数调用按钮操作中的方法:
vPanel.add(new Button("Crop", new ClickHandler()
{
public void onClick(ClickEvent event)
{
cropService.crop(getName(), (int) crop.getSelectionXCoordinate(),
(int) crop.getSelectionYCoordinate(), (int) crop.getSelectionWidth(),
(int) crop.getSelectionHeight(), new AsyncCallback<Boolean>()
{
public void onFailure(Throwable arg0)
{
// something went wrong with the call
}
public void onSuccess(Boolean arg0)
{
if (arg0)
{
// the cropped file now lives in the server
}
else
{
// an error happened in the server
}
}
});
}
}));
好了,很抱歉发了这么长的帖子,希望能有所帮助。你需要指定你想要上传的方式。如果你只是在寻找一个文件上传解决方案,你可以使用我知道的在客户端站点上传文件的唯一方法是通过FormPanel和Fileupload小部件。但这只允许上传用户通过文件管理器选择的文件,而不允许上传应用程序修改的图像——crooped图像。我正在寻找的是一种方法。你需要指定你想要上传的方式。如果你只是在寻找一个文件上传解决方案,你可以使用我知道的在客户端站点上传文件的唯一方法是通过一个FormPanel和一个Fileupload小部件。但这只允许上传用户通过文件管理器选择的文件,而不允许上传应用程序修改的图像——crooped图像。我正在寻找一种方法来实现这一点。此示例通过一个FormPanel使用FileUpload小部件,不允许动态上载修改后的图像,只允许用户直接选择图像。@Javier Vallori Amoros我编辑了我的答案,您可能想看一看。好的!谢谢你的回答。我曾经考虑过在服务器上进行crooping,但我想在客户端完成这项工作,释放服务器CPU,并最小化带宽,这意味着更便宜:D。但我认为没有一种简单而优雅的方法可以做到这一点,所以我将采用您的解决方案。又比你强!此示例通过一个FormPanel使用FileUpload小部件,该FormPanel不允许动态上载修改后的图像,只是用户直接选择的图像。@Javier Vallori Amoros我编辑了我的答案,您可能需要查看一下。好的!谢谢你的回答。我曾经考虑过在服务器上进行crooping,但我想在客户端完成这项工作,释放服务器CPU,并最小化带宽,这意味着更便宜:D。但我认为没有一种简单而优雅的方法可以做到这一点,所以我将采用您的解决方案。又比你强!