我可以使用struts2上传文件。
但我想在struts 2中显示有关上传文件的详细信息(Filename
,filesize
,filetype
)。
我正在尝试使用<s:property value="myFile" />
标记获取详细信息,
但始终显示[Object HTMLDocument]。
我的代码正在关注。
uploadfile.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<div id="success-image">
<s:property value="myFile" />
<s:head theme="ajax" />
<s:form id="asset-upload" name="asset-upload" action="uploadFile.action" method="POST" theme="ajax" enctype="multipart/form-data">
<s:file name="myFile" label="Upload Image"/>
<s:submit targets="success-image" showLoadingText="false" indicator="uploading-image" id="asdf"/>
</s:form>
</div>
</body>
</html>
struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="false" />
<constant name="struts.multipart.maxSize" value="1000000000" />
<package name="default" extends="struts-default,json-default" namespace="/">
<action name="uploadFile" class="com.isam.struts.uploadFile">
<result name="success">
<param name="myFile">myFile</param>
</result>
<result name="error">login.jsp</result>
</action>
<action name="autoCompleterList" class="com.isam.struts.AutoCompleterList">
<result type="json" />
</action>
</package>
</struts>
UploadFile.java
package com.isam.struts;
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import com.opensymphony.xwork2.ActionSupport;
public class uploadFile extends ActionSupport {
private File myFile;
private String myFileContentType;
private String myFileFileName;
private String destPath;
public String execute() {
/* Copy file to a safe location */
destPath = "C:/temp/";
try {
System.out.println("Src File name: " + myFile);
System.out.println("Dst File name: " + myFileFileName);
File destFile = new File(destPath, myFileFileName);
FileUtils.copyFile(myFile, destFile);
} catch (IOException e) {
e.printStackTrace();
return ERROR;
}
return SUCCESS;
}
public File getMyFile() {
return myFile;
}
public void setMyFile(File myFile) {
this.myFile = myFile;
}
public String getMyFileContentType() {
return myFileContentType;
}
public void setMyFileContentType(String myFileContentType) {
this.myFileContentType = myFileContentType;
}
public String getMyFileFileName() {
return myFileFileName;
}
public void setMyFileFileName(String myFileFileName) {
this.myFileFileName = myFileFileName;
}
}
那么我需要在jsp文件中添加以显示详细信息,
以及我需要在struts.xml
文件
答案 0 :(得分:1)
在uploadFile
操作中,您需要为信息和getter设置setter以在结果中显示它。
public void setUpload(File myFile) {
this.myFile= myFile;
}
public void setUploadContentType(String myFileContentType) {
this.myFileContentType= myFileContentType;
}
public void setUploadFileName(String myFileFileName) {
this.myFileFileName = myFileFileName;
}
public String getMyFileFileName() {
return myFileFileName;
}
public String getMyFileSize() {
return myFile.length();
}
public String getMyFileContentType() {
return myFileContentType;
}
它已命名为注入您可以使用
进一步显示的值<s:property value="myFileName" />
<s:property value="myFileSize" />
<s:property value="myFileContentType" />
如果文件有扩展名,您可以从myFile
进行检查。如果它有图像类型,那么您应该应用编解码器来获取它和尺寸。但是,它不是必需的。获得BufferedImage
后,您可以编写或缩放到任何图像类型或大小。
结果“成功”有什么不对。结果应该是带有id图像参数的重定向操作的正常URL,如果你想要取回它,你必须再次加载它。
答案 1 :(得分:1)
要在上传后在同一页面上显示文件详细信息,请将操作结果更改为:
<action name="uploadFile" class="com.isam.struts.uploadFile">
<result name="success">uploadfile.jsp</result>
<result name="error">login.jsp</result>
</action>
在JSP中使用<s:property>
标记来显示值:
<s:property value="myFile.length()"/>
<s:property value="myFileContentType"/>
<s:property value="myFileFileName"/>