以下为在单一环境下的结果,如果加其他的框架可能有新问题
1:commons-fileupload-1.2.1.jar commons-io-1.3.2.jar
2:<form action="fileUpload" method="post" enctype="multipart/form-data">
<input type="file" name="image">
<input type="submit" value="上传">
</form>
3:
package com.li.web.action.producttype;
import java.io.File;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.springframework.stereotype.Controller;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class FileUploadAction extends ActionSupport {
private File image;
private String imageContentType; //取得文件类型 实际上是 image+ContentType
private String imageFileName;// 文件名实际上是 image+FileName
public String getImageContentType() {
return imageContentType;
}
public void setImageContentType(String imageContentType) {
this.imageContentType = imageContentType;
}
public File getImage() {
return image;
}
public String getImageFileName() {
return imageFileName;
}
public void setImage(File image) {
this.image = image;
}
public void setImageFileName(String imageFileName) {
this.imageFileName = imageFileName;
}
@Override
public String execute() throws Exception {
// 得到站点的/images下绝对地址
String realPath = ServletActionContext.getRequest().getSession()
.getServletContext().getRealPath("/images");
System.out.println(realPath);
if (this.image != null) {
File saveFile = new File(new File(realPath),this.imageFileName);
if (!saveFile.getParentFile().exists()) {
saveFile.getParentFile().mkdirs();
}
FileUtils.copyFile(this.image,saveFile);
ActionContext.getContext().put("message","上传成功");
}
return "success";
}
}
大小设置struts.xml 以下是10M 多点其中是总量.而不是单一文件 对应大容量很容易错误.即通过web是
不稳定的,需要插件解决<constant name="struts.multipart.maxSize" value="10701096" />
原文链接:https://www.f2er.com/javaschema/287417.html