struts2的文件上传问题

为什么我按照网上那些例子做的struts2文件上传,在action中却得不到表单中的啊 :cry:

7个回答

有点怀疑你的action走没走,连个提交按钮都没有

得不到表单中的什么?

enctype="multipart/form-data" form表单中有这个属性吗

直接上代码阿,不知道你参考的哪里的上传方法。

你也没贴代码,我就不知道你错在什么地方了,给一个以前学习Struts2时上传文件的代码吧!!!
上传文件的页面:
[code="java"]
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">



Struts2 File Upload Page



文件标题:

选择文件:




[/code]
Action:
[code="java"]
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import com.opensymphony.xwork2.ActionSupport;
public class UploadAction extends ActionSupport {
private String title;
private File upload;
private String uploadContentType;
private String uploadFileName;
private String savePath;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public File getUpload() {
return upload;
}
public void setUpload(File upload) {
this.upload = upload;
}
public String getUploadContentType() {
return uploadContentType;
}
public void setUploadContentType(String uploadContentType) {
this.uploadContentType = uploadContentType;
}
public String getUploadFileName() {
return uploadFileName;
}
public void setUploadFileName(String uploadFileName) {
this.uploadFileName = uploadFileName;
}
public String getSavePath() {
return savePath;
}
public void setSavePath(String savePath) {
this.savePath = savePath;
}
public String execute() throws Exception {
System.out.println(getSavePath());
FileOutputStream fos = new FileOutputStream(getSavePath()+"\"+ getUploadFileName());
FileInputStream fis = new FileInputStream(getUpload());
byte[] buffer = new byte[1024];
int len = 0;
while ((len = fis.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
return SUCCESS;
}
}
[/code]
配置Action:[code="java"]
<?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">




D:\Program Files\Apache Software Foundation\Tomcat 5.5\webapps\Chapter4Example2\UploadFile
/succ.jsp



[/code]
web.xml:
[code="java"]
<?xml version="1.0" encoding="UTF-8"?>
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

struts
org.apache.struts2.dispatcher.FilterDispatcher


struts
/*


[/code]
需要添加一个返回成功后的页面。


中的action="upload"是提交路径把,你的xml配置了吗?
你是struts1 应该写upload.do

配置中的action="upload"是否该写成action="upload.action"

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!