struts2实现文件上传(验证过!)

jsp(因为我是要做一个wap项目,所以dtd头跟有些不一样):

 

<?xml version="1.0" encoding="UTF-8"?>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>    
<!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.0//EN" "http://www.wapforum.org/DTD/xhtml-mobile10.dtd">
<%@ page language="java" pageEncoding="UTF-8"%> 
<html>  
  <head>
   <meta http-equiv="Content-Type" content="application/vnd.wap.xhtml+xml; charset=utf-8" /> 
 <meta http-equiv="pragma" content="no-cache" /> 
 <meta http-equiv="cache-control" content="no-cache" />    
    <title>The FileUpload Demo</title>  
  </head>  
    
  <body>
  <%out.print(basePath); %> 
    <form action="<%=basePath%>m/file/uploadFile.action" method="post" enctype="multipart/form-data">  
        <p><input type="text" name="fileName" id="fileName" value=""></input>File Description</p>  
        <p><input type="file" name="upload" id="upload" value="file"></input></p>  
        <p><input type="submit" value="UpLoad"></input></p>  
    </form>  
  </body>  
</html>

 

action:

 

package com.start.action;

import java.io.*;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.start.upload.FileFiler;

public class FileUpLoadAction extends ActionSupport  {
// private FileFiler file;
//
// public FileFiler getFile() {
//  return file;
// }
//
// public void setFile(FileFiler file) {
//  this.file = file;
// }
 private String fileName;
 private File upload;
 private String uploadContentType;
 private String uploadFileName;
 private String savePath;
 
 public String getFileName() {
  return fileName;
 }
 public void setFileName(String fileName) {
  this.fileName = fileName;
 }
 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;
 }
 public String getSavePath() {
  return ServletActionContext.getRequest().getRealPath(savePath);

 }
 public void setSavePath(String savePath) {
  this.savePath = savePath;
 }
 public String fileUpload() throws Exception{
  
   //以服务器的文件保存地址和原文件名建立上传文件输出流
  
      FileOutputStream fos=new FileOutputStream(this.getSavePath()+"//"+this.getUploadFileName());
      FileInputStream fis=new FileInputStream(this.getUpload());
      byte[] buffer=new byte[1024];
      int len=0;
      while((len=fis.read(buffer))>0){
       fos.write(buffer,0,len);
      }
  
  return SUCCESS;
 }
}

 

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.i18n.encoding" value="utf-8"/>
 <constant name="struts.multipart.maxSize" value="-1"/>
 <constant name="struts.multipart.saveDir" value="/tmp"/>
 <package name="file" namespace="/m/file" extends="json-default"> 
 <global-results>
   <result type="json">
    <param name="ignoreHierarchy">false</param>
    <!-- 输出父类属性 -->
    <param name="excludeNullProperties">true</param>
    <!-- 排除空属性 -->
   </result>
   <result name="error" type="json">
    <param name="ignoreHierarchy">false</param>
    <param name="includeProperties">msg,msg.code,msg.text</param>
   </result>
  </global-results>
  <action name="uploadFile" class="fileUpLoadAction" method="fileUpload">
   <interceptor-ref name="fileUpload"> 
                <param name="allowedTypes">image/bmp, image/tmp, image/xml, image/x-png, image/gif, image/jpeg</param> 
                <param name="maximumSize">204800</param> 
            </interceptor-ref> 
            <interceptor-ref name="defaultStack"></interceptor-ref> 
            <result name="input">/index.jsp</result> 
   <param name="savePath">/upload</param>
   <result>/page/UpLoad.jsp</result>
  </action>
 </package>
</struts>

 

web.xml:

 

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
 xmlns="http://java.sun.com/xml/ns/javaee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
 
 <!-- 加载spring配置文件(as:applicationContext.xml) -->
 <listener>
    <listener-class>com.start.util.SpringHelper</listener-class>
  </listener>
  
  <!-- 指明spring配置文件在何处 -->
 <context-param>
       <param-name>contextConfigLocation</param-name>
       <param-value>/WEB-INF/classes/spring.xml</param-value>
 </context-param>
 
 <!-- 加载struts2核心 -->
  <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>
  
  <!--
 <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/m/*</url-pattern>
  </filter-mapping>
  
  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/l/*</url-pattern>
  </filter-mapping>
  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/c/*</url-pattern>
  </filter-mapping>
  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/page/*</url-pattern>
  </filter-mapping>
  -->
  <!-- JSTL -->
  <jsp-config>
   <taglib>
   <taglib-uri>http://java.sun.com/jstl/core</taglib-uri>
   <taglib-location>/WEB-INF/c.tld</taglib-location>
  </taglib>
  <taglib>
   <taglib-uri>http://java.sun.com/jstl/fmt</taglib-uri>
   <taglib-location>/WEB-INF/fmt.tld</taglib-location>
  </taglib>
   </jsp-config>
   <session-config>  
  <session-timeout>240</session-timeout>  
 </session-config> 
</web-app>
web.xml有加载了我自己的东西,用的时候要把不需要的删掉。

 

测试时候当时有个问题:

java.io.FileNotFoundException: /tmp/upload__3e019d96_12ec6707d88__7fff_00000001.tmp (系统找不到指定的文件。)
java.io.FileInputStream.open(Native Method)
java.io.FileInputStream.<init>(FileInputStream.java:106)
com.start.action.FileUpLoadAction.fileUpload(FileUpLoadAction.java:62)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:597)

我 的错误是这样的,我测试时因为规定了各式,就新建了个文本文件,改了下各式,但是文件大小是0字节,结果报错了,改个有大小的就ok了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值