文件上传
发布时间:2023-09-13 14:51:12 170
相关标签:
在stucts中上传文件步骤:
第一步:在WEB-INF/lib下加入commons-fileupload-1.2.1.jar、commons-io-1.3.2.jar。这两个文件可以从http://commons.apache.org/下载。
第二步:编写提交表单:index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
第三步:编写Action
public class uplodeFile {
private File uploadImage;//这个名字和相同
private String uplodeFileContentType;//同上-ContentType这必须这么写
private String uploadImageFileName;//同上-FileName;这必须这么写
private String path;
public String getPath() {
return path;
}
public File getUploadImage() {
return uploadImage;
}
public void setUploadImage(File uploadImage) {
this.uploadImage = uploadImage;
}
public String getUplodeFileContentType() {
return uplodeFileContentType;
}
public void setUplodeFileContentType(String uplodeFileContentType) {
this.uplodeFileContentType = uplodeFileContentType;
}
public String getUploadImageFileName() {
return uploadImageFileName;
}
public void setUploadImageFileName(String uploadImageFileName) {
this.uploadImageFileName = uploadImageFileName;
}
public String upload() throws Exception
{
String realpath = ServletActionContext.getServletContext().getRealPath("/images");
File file = new File(realpath);
if(!file.exists()) file.mkdirs();
path=file.getAbsolutePath();
FileUtils.copyFile(uploadImage, new File(file, uploadImageFileName));
return "success";
}
}
第四步:配置struts.xml文件
第五步:部署
第六步:访问:http://localhost:8080/Structs2/index.jsp
注意:index.jsp在WebRoot目录下
文章来源: https://blog.51cto.com/u_13991401/5883799
特别声明:以上内容(图片及文字)均为互联网收集或者用户上传发布,本站仅提供信息存储服务!如有侵权或有涉及法律问题请联系我们。
举报