struts实现upload文件上传

1.。。。。。。。。。。。。。。

导入struts包

 

 

 

 

 

 

 

 

2。。。。。。。。。。。。。。。。

配置web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>NewJava</display-name>
<filter>
  <filter-name>struts2</filter-name>
  <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
  <filter-name>struts2</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
  <welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>


 

 

 

3。。。。。。。。。。。。。。。。。。。。。

上传JSP页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@ taglib prefix="s" uri="/struts-tags" %>
<!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>文件上传</title>
</head>
<body>
<div align="center">
<br/><br/>
<img alt="文件上传" src="images/upload.png">
<s:form method="post" action="Upload" theme="simple" enctype="multipart/form-data"> 
文件名称:<s:textfield name="title"></s:textfield>
文件地址:<s:file name="upload"></s:file>
<s:submit value="上传"></s:submit>
</s:form>
</div>
</body>
</html>




 
 

4.。。。。。。。。。。。。。。。。。。。。。编写Action

package com.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class UploadAction extends ActionSupport {

private String title;
private File upload;
private String uploadContentType;
// 封装上传文件名的属性
private String uploadFileName;
// 直接在struts.xml文件中配置的属性

public String execute() throws Exception {
  //以服务器的文件保存地址和原文件名建立上传文件输出流  
String path = ServletActionContext.getServletContext().getRealPath ("/load/");//获取服务器存放物理路径
System.out.println(path+ getUploadFileName());
FileOutputStream fos = new FileOutputStream(path+"\\"+ getUploadFileName());
FileInputStream fis = new FileInputStream(getUpload()); 
byte[] buffer = new byte[1024]; 
int len = 0; 
while ((len = fis.read(buffer)) > 0)  
{  fos.write(buffer , 0 , len);  
} 
System.out.println("上传成功");
System.out.println(path+"\\"+ getUploadFileName());
  return SUCCESS;
}

public String getTitle() {
  return title;
}
public void setTitle(String title) {
  this.title = title;
}
public File getUpload() {
  return upload;
}
public void setUpload(File upload) {
  this.upload = upload;
}
public String getUploadContentType() {
  return uploadContentType;
}
public void setUploadContentType(String uploadContentType) {
  this.uploadContentType = uploadContentType;
}
public String getUploadFileName() {
  return uploadFileName;
}
public void setUploadFileName(String uploadFileName) {
  this.uploadFileName = uploadFileName;
}

}





5。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。

配置struts.xml

 


<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
     "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
<constant name="struts.devMode" value="true" />

<package name="example" extends="struts-default">

  <action name="Upload" class="com.action.UploadAction">
   <result >/download.jsp</result>
  </action>
</package>
</struts>


6。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。

看看目录结构 运行吧  文件上传到load文件夹下面  可能有点慢

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值