<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML>
<html>
<head>
<title>Upload File</title>
</head>
<body>
<form action="UploadFile" method="post" enctype="multipart/form-data">
<!--文件域-->
<input type="file" name="source" /> <input type="submit" value="上传">
</form>
</body>
</html>
<action name="UploadFile" class="com.lidi.action.UploadAction">
<result name="success">/uploadResult.jsp</result>
<!--fileUpload拦截器,可用于限制上传文档的类型和文档大小 -->
<interceptor-ref name="fileUpload">
<!-- 限制文件大小20M,单位为字节 -->
<param name="maximumSize">20971520</param>
</interceptor-ref>
<!--默认拦截器,必须声明在fileUpload拦截器之后 -->
<interceptor-ref name="defaultStack" />
</action>
<constant name="struts.multipart.maxSize" value="21000000"/>
<!-- 设置只允许上传图片文件 --> <intercepter-ref name="fileUpload"> <param name="allowedTypes">image/bmp, image/x-png, image/gif, image/jpeg</param> </intercepter-ref> <interceptor-ref name="defaultStack" />
<!-- 设置只允许上传word文档 --> <intercepter-ref name="fileUpload"> <param name="allowedTypes">application/msword, application/vnd.openxmlformats-officedocument.wordprocessingml.document</param> </intercepter-ref> <interceptor-ref name="defaultStack" />
private File abc; private String abcFileName; private String abcContentType;
public class UploadAction extends ActionSupport implements ServletContextAware {
/**
* 省略其它代码...
*/
private ServletContext context;
public ServletContext getContext() {
return context;
}
public void setContext(ServletContext context) {
this.context = context;
}
@Override
public void setServletContext(ServletContext context) {
this.context = context;
}
}
String path = context.getRealPath("/upload");// 重要:斜杠不能少
/*将文件上传到upload文件夹下*/ File savefile = new File(path, sourceFileName); FileUtils.copyFile(source, savefile);
package com.lidi.action;
import java.io.File;
import java.io.IOException;
import javax.servlet.ServletContext;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.util.ServletContextAware;
import com.opensymphony.xwork2.ActionSupport;
public class UploadAction extends ActionSupport implements ServletContextAware {
/**
*
*/
private static final long serialVersionUID = 1L;
private File source;// 待上传文件
private String sourceFileName;// 待上传文件的文件名
private String sourceContentType; // 待上传文件的文件类型
private ServletContext context; // 重要
/* 重要 */
public ServletContext getContext() {
return context;
}
public void setContext(ServletContext context) {
this.context = context;
}
/* getters & setters */
public File getSource() {
return source;
}
public void setSource(File source) {
this.source = source;
}
public String getSourceFileName() {
return sourceFileName;
}
public void setSourceFileName(String sourceFileName) {
this.sourceFileName = sourceFileName;
}
public String getSourceContentType() {
return sourceContentType;
}
public void setSourceContentType(String sourceContentType) {
this.sourceContentType = sourceContentType;
}
@Override
public void setServletContext(ServletContext context) {
this.context = context;
}
public String execute() throws IOException {
/*获取存放上传文件的路径:项目根目录upload文件夹*/
String path;
path = context.getRealPath("/upload");// 重要:斜杠不能少
System.out.println(path);
/*将文件上传到upload文件夹下*/
File savefile = new File(path, sourceFileName);
FileUtils.copyFile(source, savefile);
System.out.println(savefile.getAbsolutePath());
return SUCCESS;
}
}
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib uri="/struts-tags" prefix="s"%> <!DOCTYPE HTML> <html> <head> <title>Upload Result</title> </head> <body> <p>文件名:<s:property value="sourceFileName" /></p> <p>文件类型:<s:property value="sourceContentType" /></p> <p>文件:<a href="upload/<s:property value="sourceFileName" />"><s:property value="sourceFileName" /></a></p> </body> </html>
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有