我正在開發一個Web應用程序,用戶選擇一個圖像,對其進行裁剪並最終將其上傳到服務器。因此,與一個FileUpload控件,我允許用戶選擇圖像源,獲得其路徑,並與構造上傳GWT中的裁剪圖像
Image(java.lang.String url, int left, int top, int width, int height);
我得到裁剪後的圖像對象。
但現在,我不知道如何上傳到服務器的圖像。有人知道解決方案嗎?
我正在開發一個Web應用程序,用戶選擇一個圖像,對其進行裁剪並最終將其上傳到服務器。因此,與一個FileUpload控件,我允許用戶選擇圖像源,獲得其路徑,並與構造上傳GWT中的裁剪圖像
Image(java.lang.String url, int left, int top, int width, int height);
我得到裁剪後的圖像對象。
但現在,我不知道如何上傳到服務器的圖像。有人知道解決方案嗎?
你可以找到一個很好的例子,如何上傳文件到服務器here。
編輯
你需要做的是將圖像上傳到服務器,在客戶端檢索它,做視覺上的裁剪客戶端,發送裁剪參數到服務器,最後做實際在服務器中裁剪。這是我能做到它開始從項目上面提到的:
vPanel.add(new Button("Submit", new ClickHandler() {
public void onClick(ClickEvent event) {
form.submit();
}
}));
一旦用戶選擇圖像,我們用的FileUpload上傳,並在服務器上我們把它保存在一個目錄:
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);
}
,我們需要有一個服務來獲取上傳的圖片,所以我們增加一個servlet,需要一個GET方法,並返回圖像:當上載已
protected void doGet(HttpServletRequest req,
HttpServletResponse resp) throws ServletException,
IOException
{
resp.setContentType("image/jpeg");
ServletOutputStream out = resp.getOutputStream();
BufferedInputStream bis= new BufferedInputStream(new FileInputStream(<images-path>+req.getParameter("name")));
BufferedOutputStream bos = new BufferedOutputStream(resp.getOutputStream());
int ch;
while((ch=bis.read())!=-1)
{
bos.write(ch);
}
bis.close();
bos.flush();
bos.close();
out.close();
}
返回到客戶端完成後,我們希望檢索上傳圖像的副本,以便添加表單提交處理程序。我用這gwt cropping library的視覺步:
form.addSubmitCompleteHandler(new FormPanel.SubmitCompleteHandler()
{
public void onSubmitComplete(SubmitCompleteEvent event)
{
Element label = DOM.createLabel();
label.setInnerHTML(event.getResults());
String result = label.getInnerText(); // result contains the name of the image in the server
final GWTCropper crop = new GWTCropper(GWT.getModuleBaseURL()+"image?name="+result);
crop.setAspectRatio(1);
vPanel.add(crop);
}
}
現在我們要添加一個裁剪服務,讓實際種植在我使用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小部件,該小部件不允許動態上載修改後的圖像,只是由用戶直接選擇的圖像。 –
@Javier Vallori Amoros我編輯了我的答案,你可能想看看。 – amaurs
好的!謝謝你的回答阿毛裏。我曾考慮過這個選擇(在服務器上進行操作),但我想在客戶端做這項工作,釋放服務器CPU,並儘量減少帶寬(這意味着更便宜:D)。但我認爲這並不是一種簡單而優雅的方式,所以我會採取您的解決方案。再比你一次! –
您需要指定上傳方式。 –
如果你只是在尋找一個文件上傳解決方案,你可以使用[Apache Commons File Upload](http://commons.apache.org/proper/commons-fileupload/using.html) – Churro
我知道上傳一個唯一的方法文件(在客戶端站點中)通過FormPanel和Fileupload小部件。但是,這隻允許上傳用戶使用文件管理器選擇的文件,而不是由應用程序修改的圖像(crooped圖像)。我正在尋找的是一種方法來做到這一點。 –