gpt4 book ai didi

java - 在不使用数据源的情况下以 SmartGWT 的动态形式上传文件时从 GWT 中的 Servlet 获取回调

转载 作者:行者123 更新时间:2023-11-30 11:49:13 25 4
gpt4 key购买 nike

我正在使用 SmartGWT 动态表单 submitForm() 在客户端实现文件上传功能,代码如下:

final String DEFAULT_FILE_UPLOAD_SERVICE_PATH = "upload";
final String TARGET = "uploadTarget";

VLayout body = new VLayout();

uploadForm = new DynamicForm();

// initialise the hidden frame
NamedFrame frame = new NamedFrame(TARGET);
frame.setWidth("1px");
frame.setHeight("1px");
frame.setVisible(false);

uploadForm.setEncoding(Encoding.MULTIPART);
uploadForm.setMethod(FormMethod.POST);
// set the (hidden) form target
uploadForm.setTarget(TARGET);

uploadForm.setAction(DEFAULT_FILE_UPLOAD_SERVICE_PATH);

// initialise the File name field
uploadItem = new UploadItem("filename");
uploadItem.setName("filename");
uploadItem.setTitle("File name");

// set the fields into the form
uploadForm.setFields(uploadItem);

// add the Upload Form and the (hidden) Frame to the main layout container
body.addMember(uploadForm);
body.addMember(frame);

在服务器端我有一个 servlet 来处理使用 Apache 文件上传库的文件上传请求,这里是代码:

@Singleton
@SuppressWarnings("serial")
public class FileUploadServlet extends HttpServlet {
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.process(request, response);
}

@Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.process(request, response);
}

private void process(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// check that we have a file upload request
if (ServletFileUpload.isMultipartContent(request)) {
processFiles(request, response);
}
}

private File tmpDir;
private static final String DESTINATION_DIR_PATH = "/files/upload";
private File destinationDir;

public void init(ServletConfig config) throws ServletException {
super.init(config);

tmpDir = new File(((File) getServletContext().getAttribute("javax.servlet.context.tempdir")).toString());

if (!tmpDir.isDirectory()) {
throw new ServletException(tmpDir.toString() + " is not a directory");
}

Log.debug("tmpDir: " + tmpDir.toString());

String realPath = getServletContext().getRealPath(DESTINATION_DIR_PATH);
destinationDir = new File(realPath);

if (!destinationDir.isDirectory()) {
throw new ServletException(DESTINATION_DIR_PATH + " is not a directory");
}
}

private void processFiles(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

// create a factory for disk-based file items
DiskFileItemFactory factory = new DiskFileItemFactory();

// set the size threshold, above which content will be stored on disk
factory.setSizeThreshold(1 * 1024 * 1024); // 1 MB

// set the temporary directory (this is where files that exceed the threshold will be stored)
factory.setRepository(tmpDir);

// create a new file upload handler
ServletFileUpload upload = new ServletFileUpload(factory);

try {
// parse the request
List<?> items = upload.parseRequest(request);

// process the uploaded items
Iterator<?> itr = items.iterator();

while (itr.hasNext()) {
FileItem item = (FileItem) itr.next();

// write the uploaded file to the application's file staging area
File file = new File(destinationDir, item.getName());
item.write(file);
}

} catch (FileUploadException e) {
Log.error("Error encountered while parsing the request", e);
} catch (Exception e) {
Log.error("Error encountered while uploading file", e);
}
}
}

使用上面的代码,文件上传工作正常。我的问题是我不知道如何从 servlet 取回电话。一tutorial说使用隐藏框架,但我不知道该怎么做。谁能帮我解决这个问题?谢谢。

最佳答案

您需要写出一个标记作为您的服务器响应,其中包含将在框架内执行的 JavaScript 代码。此代码将需要跳出框架以调用您已放入主页的某些函数(例如 top.someFunction())。要在主页中设置函数,请使用 JSNI 将 JavaScript 函数附加到窗口(JSNI 中的 $wnd)。

请注意,如果您使用的是 SmartGWT Pro+,则无需执行任何操作 - 此解决方案仅适用于使用 LGPL 版本的用户。

关于java - 在不使用数据源的情况下以 SmartGWT 的动态形式上传文件时从 GWT 中的 Servlet 获取回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8488078/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com