文件上传

来源:互联网 发布:淘宝小二电话 编辑:程序博客网 时间:2024/06/10 15:53

觉得这个多文件上传写的不错大家可以去看看:http://blog.csdn.net/qq_22498277/article/details/51345283

单文件下载:https://jingyan.baidu.com/album/4853e1e577056a1909f72630.html?picindex=1

单文件上传

fileup.jsp

<%@ 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>   <div align="Center">   <form action="myfile!upload.action" method="post"  enctype="multipart/form-data">   <table>   <tr>   <td>文件上传:</td>   <td><input name="myf" type="file" /> </td>   </tr>   <tr align="Center">   <td colspan="2">   <input type="submit" value="上传"/>    </td>   </tr>   </table>   </form>   </div>  </body></html>

FileAction.java

package com.zking.action;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import org.apache.log4j.Logger;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionSupport;/** * 上传文件 * @author Administrator * */public class FileAction extends ActionSupport {//日志private Logger log = Logger.getLogger(FileAction.class);//文件对象属性private File myf;//文件类型private String myfContentType;//文件名字private String myfFileName;public String upload() throws Exception {log.info("到了FileAction。。。。。");log.info("文件:"+this.myf);log.info("文件类型:"+this.myfContentType);log.info("文件名字:"+this.myfFileName);//输入流FileInputStream fis = new FileInputStream(myf);//输出流String path = ServletActionContext.getServletContext().getRealPath("/upload");path = path+"\\"+this.myfFileName;FileOutputStream fos = new FileOutputStream(path);int n ;while((n=fis.read())!=-1){fos.write(n);}//关闭流fis.close();fos.close();return "success";}public File getMyf() {return myf;}public void setMyf(File myf) {this.myf = myf;}public String getMyfContentType() {return myfContentType;}public void setMyfContentType(String myfContentType) {this.myfContentType = myfContentType;}public String getMyfFileName() {return myfFileName;}public void setMyfFileName(String myfFileName) {this.myfFileName = myfFileName;}}
struts.xml

<!-- 打开方法的动态调用 --><constant name="struts.enable.DynamicMethodInvocation" value="true" /><package name="mypackage" extends="struts-default" namespace="/"><action name="myfile" class="com.zking.action.FileAction"><result name="success">file_success.jsp</result></action>
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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">  <display-name>G160628_struts2_001</display-name>  <welcome-file-list>    <welcome-file>index.html</welcome-file>    <welcome-file>index.htm</welcome-file>    <welcome-file>index.jsp</welcome-file>    <welcome-file>default.html</welcome-file>    <welcome-file>default.htm</welcome-file>    <welcome-file>default.jsp</welcome-file>  </welcome-file-list><!-- struts2核心过滤器配置 -->  <filter><filter-name>struts2</filter-name><filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class></filter><filter-mapping><filter-name>struts2</filter-name><url-pattern>*.action</url-pattern></filter-mapping></web-app>
导入jar包:可以去struts.apache.org进行下载
(1)commons-fileupload-1.3.2.jar    文件上传时需要使用
(2)commons-io-2.2.jar        Java io扩展
(3)commons-lang3-3.2.jar        包含了一些数据类型的工具类
(4)commons-logging-1.1.3.jar        日志处理
(5)freemarker-2.3.22.jar        Struts2的标签模板使用类库
(6)javassist-3.11.0.GA.jar        对字节进行处理
(7)ognl-3.0.19.jar            Struts2使用的一种表达式语言类库
(8)struts2-core-2.3.33.jar          Struts2框架的核心类库
(9)xwork-core-2.3.33.jar           XWork类库,Struts2的构建基础

(10)log4j-1.2.17.jar             日志



原创粉丝点击