package web;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream; import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport; public class UploadDemoAction extends ActionSupport {
private String username;
private File myfile;
private String myfileContentType;
private String myfileFileName;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public File getMyfile() {
return myfile;
}
public void setMyfile(File myfile) {
this.myfile = myfile;
}
public String getMyfileContentType() {
return myfileContentType;
}
public void setMyfileContentType(String myfileContentType) {
this.myfileContentType = myfileContentType;
}
public String getMyfileFileName() {
return myfileFileName;
}
public void setMyfileFileName(String myfileFileName) {
this.myfileFileName = myfileFileName;
}
public String upload(){
try {
String path = ServletActionContext.getServletContext().getRealPath(
"/upload/file");
System.out.println("path========"+path);
InputStream input = new FileInputStream(myfile);
System.out.println("myfileFileName===="+myfileFileName);
String allpath=path+"\\"+myfileFileName;
System.out.println("allpath===="+allpath);
OutputStream output = new FileOutputStream(allpath);
IOUtils.copy(input, output);
return SUCCESS;
} catch (Exception e) {
e.printStackTrace();
return INPUT;
}
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="upload.action" method="post" enctype="multipart/form-data">
用户名:<input type="text" name="username"/>
文件:<input type="file" name="myfile" />
<input type="submit" value="提交"/>
</form>
</body>
</html>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<!-- 忽略struts格式 -->
<constant name="struts.ui.theme" value="simple"></constant>
<constant name="struts.configuration.xml.reload" value="true"></constant>
<package name="houseuser" extends="struts-default" namespace="/"> <!-- 上传图片示例-->
<action name="upload" class="web.UploadDemoAction" method="upload">
<result >success.jsp</result>
</action>
</package> </struts>

最新文章

  1. 【解决方案】 org.springframework.beans.factory.BeanCreationException: Error creating bean with name &#39;userHandler&#39;: Injection of resource dependencies failed;
  2. django manytomany
  3. Mac PHP 环境配置+渗透环境
  4. 基础笔记3(一)(String StringBuilder StringBuffer 数组)
  5. C#语言的Image和byte数组的互相转换
  6. (组合数学3.1.2.2)POJ 2084 Game of Connections(卡特兰数公示的实现)
  7. 阿里云服务器上使用iptables设置安全策略
  8. Linux中MySQL配置文件my.cnf参数优化
  9. 可视化编程开发板TurnipBit支持LED亮度可调功能
  10. Mac 下 Chrome多个Tab之间切换
  11. [Vue] vue跳转外部链接
  12. 搭建idea下的vue工程
  13. Tomcat/7.0.81 远程代码执行漏洞复现
  14. Java7和8在虚拟机上的差异:Perm Generation vs. Metaspace
  15. IOS多线程处理
  16. 001-分布式理论-CAP定理
  17. NET设计模式 第二部分 创建型模式(6):创建型模式专题总结(Creational Pattern)
  18. Office 手动kms激活方法
  19. net 表格控件
  20. 【洛谷 P4072】 [SDOI2016]征途(斜率优化)

热门文章

  1. ajax总结:
  2. Oracle Sales Cloud:管理沙盒(定制化)小细节1——利用公式创建字段并显示在前端页面
  3. docker通过iptables修改或新增镜像映射端口
  4. MVC下压缩输入的HTML内容
  5. GitLab CI持续集成配置方案
  6. 【译】Spring 4 自动装配、自动检测、组件扫描示例
  7. 论Pair的重要性
  8. Quartz2.0以上版本的单机和集群
  9. Scala 数据类型(二)
  10. 机器学习实战笔记(Python实现)-07-分类性能度量指标