您好我想在服务器上上传多个文件,例如一个excel和两个xml文件,但没有为操作定义结果,并且正在显示结果输入错误。我正在使用struts2框架。
我的Jsp页面
<form action ="myInput" enctype="multipart/form-data" method="post" >
<input type="file" name="fileUpload[]" />
<input type="file" name="fileUpload[]" />
<input type="file" name="fileUpload[]" />
<input type="submit" value="Upload Records" />
</form>
我的struts.xml -
<package name="links" extends="struts-default, json-default" namespace="/">
<action name="myInput" class="com.action.MyAction" >
<interceptor-ref name="linksAccessRequired"/>
<interceptor-ref name="fileUpload">
<param name="allowedTypes">*/*</param>
<param name="maximumSize">10485760</param> <!-- 10MB -->
</interceptor-ref>
<interceptor-ref name="defaultStack" />
<result name="success" type="json"/>
<result name="login" type="redirect">/</result>
</action>
</package>
我的操作文件 -
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;
import java.io.File;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.interceptor.ServletRequestAware;
public class MyAction extends ActionSupport implements ServletRequestAware {
/**
*
*/
private static final long serialVersionUID = 1L;
private boolean result = false;
private String status = null;
private String msg = null;
HttpServletRequest request = null;
private File[] fileUpload;
private String[] fileUploadFileName;
private String[] fileUploadContentType;
public File[] getFileUpload() {
return fileUpload;
}
public void setFileUpload(File[] fileUploads) {
this.fileUpload = fileUploads;
}
public String[] getFileUploadFileName() {
return fileUploadFileName;
}
public void setFileUploadFileName(String[] fileUploadFileNames) {
this.fileUploadFileName = fileUploadFileNames;
}
public String[] getFileUploadContentType() {
return fileUploadContentType;
}
public void setFileUploadContentType(String[] fileUploadContentTypes) {
this.fileUploadContentType = fileUploadContentTypes;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public String execute() {
System.out.println("Something");
String saveDirectory = "/mypath/excels/Output/"+request.getSession().getAttribute("my")+"/myOut/";
if(this.doUpload(fileUpload, saveDirectory))
{
this.setStatus("Success");
this.setMsg("Files have been upload successfully !");
}
else
{
this.setStatus("Failled");
this.setMsg("Files have not been upload successfully !");
}
return Action.SUCCESS;
}
public boolean doUpload(File[] fileUpload, String saveDirectory) {
// copy the uploaded files into pre-configured location
for (int i = 0; i < fileUpload.length; i++) {
File uploadedFile = fileUpload[i];
String fileName = fileUploadFileName[i];
File destFile = new File(saveDirectory + File.separator + fileName);
try {
FileUtils.copyFile(uploadedFile, destFile);
} catch (IOException ex) {
System.out.println("Could not copy file " + fileName);
ex.printStackTrace();
}
}
return true;
}
@Override
public void setServletRequest(HttpServletRequest request) {
this.request = request;
}
}
为什么我得到没有为动作和结果输入错误定义的结果? 请建议我解决这个问题。 感谢
答案 0 :(得分:0)
首先阅读how INPUT result works,然后在操作配置中正确添加一个;
此时,您应该能够发现自己的错误,即:
<form action="myInput" enctype="multipart/form-data" method="post" >
<input type="file" name="fileUpload[]" />
<input type="file" name="fileUpload[]" />
<input type="file" name="fileUpload[]" />
<input type="submit" value="Upload Records" />
</form>
虽然它应该是:
<form action="myInput" enctype="multipart/form-data" method="post" >
<input type="file" name="fileUpload[0]" />
<input type="file" name="fileUpload[1]" />
<input type="file" name="fileUpload[2]" />
<input type="submit" value="Upload Records" />
</form>
甚至更好:
<form action="myInput" enctype="multipart/form-data" method="post" >
<input type="file" name="fileUpload" multiple />
<input type="submit" value="Upload Records" />
</form>
使用Struts标签将是:
<s:form action="myInput" enctype="multipart/form-data" method="post" >
<s:file name="fileUpload" multiple />
<s:submit value="Upload Records" />
</s:form>
附注:如果您有兴趣知道如何使用dropzone.js this may be worth reading在Struts2中执行多次上传。