加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

单一文件上传

发布时间:2020-12-13 20:21:28 所属栏目:百科 来源:网络整理
导读:以下为在单一环境下的结果,如果加其他的框架可能有新问题 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="上传

以下为在单一环境下的结果,如果加其他的框架可能有新问题
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" />

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读