5,655
社区成员
发帖
与我相关
我的任务
分享
<s:actionerror/>
<s:form action="uploadFile" enctype="multipart/form-data" method="post">
<s:textfield name="name" label="文件名"></s:textfield>
<s:file name="photo" label="图片"></s:file>
<s:submit value="上传"></s:submit>
</s:form>
<struts>
<constant name="struts.multipart.maxSize" value="10000000" />
<package name="pk1" extends="struts-default">
<action name="uploadFile" class="com.javaWeb.action.Upload1Action" method="upload">
<result name="success">/success.jsp</result>
<result name="error">/error.jsp</result>
<result name="input">/error.jsp</result>
</action>
</package>
</struts>
public class Upload1Action extends ActionSupport {
private static final long serialVersionUID = 1L;
private String name;
private File photo; //必须是File类型,名字对应表单的上传输入域
private String photoFileName; //上传文件的文件名。XXXFileName固定写法
private String photoContentType;//上传文件的MIME类型。XXXContentType固定写法
public String upload() throws Exception{
System.out.println(name);
//完成上传
ServletContext sc = ServletActionContext.getServletContext();
String directory = sc.getRealPath("/files");//得到存放文件的真实目录
//构建目标文件
File target = new File(directory, photoFileName);
FileUtils.copyFile(photo, target);
return SUCCESS;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public File getPhoto() {
return photo;
}
public void setPhoto(File photo) {
this.photo = photo;
}
public String getPhotoFileName() {
return photoFileName;
}
public void setPhotoFileName(String photoFileName) {
this.photoFileName = photoFileName;
}
public String getPhotoContentType() {
return photoContentType;
}
public void setPhotoContentType(String photoContentType) {
this.photoContentType = photoContentType;
}
}