gpt4 book ai didi

java - 从表单中保存 HTTP POST 数据以便通过 Java 多次使用它,可能吗?

转载 作者:太空宇宙 更新时间:2023-11-04 14:08:26 26 4
gpt4 key购买 nike

也许我没有以正确的方式提出问题,但这是另一种尝试,这次我会更彻底。我做了一个 super 简单的项目来举例说明我想要实现的目标。

我使用的是 netbeans 和 struts 1.3。

我有这个非常简单的表格:

 <html:form action="/firstUse.do" enctype="multipart/form-data" method="post">
<html:hidden property="anyname" value="whatevername" />
<input type="file" name="file" />
<input type="submit" value="Submit" />
</html:form>

我有我的 ActionForm:

public class MyActionForm extends org.apache.struts.action.ActionForm {

private org.apache.struts.upload.FormFile file;
private String anyname;

public FormFile getFile() {
return file;
}

public void setFile(FormFile file) {
this.file = file;
}

public String getAnyname() {
return anyname;
}

public void setAnyname(String anyname) {
this.anyname = anyname;
}

public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
ActionErrors errors = new ActionErrors();

int tamArchivo = file.getFileSize();
String nomArchivo = file.getFileName();

String extArchivo = nomArchivo.substring(nomArchivo.indexOf(".") + 1).toUpperCase();

if (tamArchivo <= 0) {
errors.add("", new ActionMessage("documentosEntregados.imagenVacia"));
} else if (tamArchivo >= 1048576) { // 1 Mega
errors.add("", new ActionMessage("documentosEntregados.imagenGrande"));
}
return errors;
}
}

我有我的行动:

public class FirstUse extends org.apache.struts.action.Action {

private static final String SUCCESS = "success";
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
MyActionForm myf = (MyActionForm) form;
String name;
String filename;

name = myf.getAnyname();
FormFile file = myf.getFile();
filename = file.getFileName();

System.out.println("FirstUse Action");
System.out.println(name);
System.out.println(filename);
return mapping.findForward(SUCCESS);
}
}

我有我的过滤器:

    public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain)
throws IOException, ServletException {

Throwable problem = null;
try {

String strPath = ((HttpServletRequest) request).getServletPath();

if (strPath.endsWith("firstUse.do")) {

System.out.println("Entered filter first");

HttpServletRequest httpReq = (HttpServletRequest) request;
HttpServletRequestWrapper reqWrapper = new HttpServletRequestWrapper(httpReq);

DiskFileItemFactory factory = new DiskFileItemFactory();

// Configure a repository (to ensure a secure temp location is used)
ServletContext servletContext = filterConfig.getServletContext();
File repository = (File) servletContext.getAttribute("javax.servlet.context.tempdir");
factory.setRepository(repository);

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

List<FileItem> items = upload.parseRequest(reqWrapper);

Iterator<FileItem> iter = items.iterator();
if (!iter.hasNext()) {
System.out.println("iter super empty");
} else {
System.out.println("iter not super empty");
while (iter.hasNext()) {
FileItem item = iter.next();
if (item.isFormField()) {
System.out.println("Field name: " + item.getFieldName() + " Field value: " + item.getString());
} else {
System.out.println("File found");
}
}
}

System.out.println("Finished FirstUse, Now I want to do a second use");

chain.doFilter(reqWrapper, response);
}else{
chain.doFilter(request, response);
}

} catch (Throwable t) {
System.out.println("reqWrapper emtpy or not accessible");
problem = t;
t.printStackTrace();
}
if (problem != null) {
if (problem instanceof ServletException) {
throw (ServletException) problem;
}
if (problem instanceof IOException) {
throw (IOException) problem;
}
}
}

当我按下提交时,过滤器被调用,我将我的请求包装在这里:

    HttpServletRequest httpReq = (HttpServletRequest) request;
HttpServletRequestWrapper reqWrapper = new HttpServletRequestWrapper(httpReq);

现在,关注下一个,这是我的问题。我将解释这种行为以及我想要实现的目标。

如果我先调用下一行:

chain.doFilter(reqWrapper, response);

该操作正确执行,我在系统日志中获取了我的数据:

FirstUse Action
whatevername
Elastix.jpg
Finished FirstUse, Now I want to do a second use
iter super empty

但是当我尝试使用 apache commons 迭代请求时,这里:

List<FileItem> items = upload.parseRequest(reqWrapper);

Iterator<FileItem> iter = items.iterator();
if (!iter.hasNext()) {
System.out.println("iter super empty");
}

POST 数据不可访问,所以我得到一个空迭代器,流被关闭,这是抛出的 apache 常见文件上传异常。

现在,如果我首先执行迭代,而不是调用 chain.doFilter,迭代就会完成,并且我会得到结果:

iter not super empty 
Field name: anyname
Field value: whatevername
File found

但是当它再次到达 chain.doFilter 时,POST 数据不可访问,因此当我尝试执行该操作时,我收到空指针异常,如果打开验证表单,它甚至会在验证之前崩溃,因为表单值为空,发布数据消失了。

所以,在阅读了很多天和很多小时之后,我开始明白我正在将 HttpServletRequest 有效地保存在我的包装器中,但是,当我尝试再次使用它时,客户端当然不会再次发送信息,因为它已经发送过一次,所以这里的问题是:

我如何模仿这种行为?我如何存储 POST 数据以使用它两次,就像我在过滤器中尝试做的那样。

最佳答案

你试过这个吗:chain.doFilter(请求,响应);

而不是这个:chain.doFilter(reqWrapper, 响应);

关于java - 从表单中保存 HTTP POST 数据以便通过 Java 多次使用它,可能吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28620597/

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