上传功能的实现(Struts2)

上传功能的实现(Struts2)


在WebRoot根目录新建一个images文件夹

upload.jsp

<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<html>
  <head>
    <base href="<%=basePath%>">
  </head>
  
  <body bgcolor="green"> 
      <s:form name="" action="momo/upload" enctype="multipart/form-data">
          图片路径:<s:file name="myFile" label="浏览照片"/>
              <s:submit value="上传"/>
      </s:form>
      
  </body>
</html>



struts.xml

<?xml version="1.0" encoding="GBK" ?>
<!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="gbk"/>
  <constant name="struts.custom.i18n.resources" value="ApplicationResources"/>
  
  <package name="mm" extends="struts-default" namespace="/momo">
   <action name="upload" class="com.org.upload.action.UploadAction">
   
   <interceptor-ref name="defaultStack">
  		<param name="fileUpload.maximumSize">10240000</param>    <!--  1024b=1Kb   (10000KB/1024)M  -->
  		<param name="fileUpload.allowedTypes">
  		image/bmp,image/png,image/gif,image/jpeg,image/jpg,image/pjpeg,
  		application/msword,text/plain,application/vnd.ms-excel  <!-- .doc  .txt  .xls -->
  		</param>
   </interceptor-ref>
   
   <result name="input">/upload.jsp</result>
   <result name="success">/success.jsp</result>
   <param name="savePath">/images</param> 
   
   </action>
   </package>
  </struts>


UploadAction.java

package com.org.upload.action;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.sql.Timestamp;
import java.util.Date;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.Action;

public class UploadAction implements Action{
	private File myFile; //上传文件的file对象
	private String myFileFileName;//上传文件名    
	private String myFileContentType;//上传文件的MINE类型   jpeg/jpg
	private String savePath;//保存路径
	
	public File getMyFile() {
		return myFile;
	}
	public void setMyFile(File myFile) {
		this.myFile = myFile;
	}
	public String getMyFileFileName() {
		return myFileFileName;
	}
	public void setMyFileFileName(String myFileFileName) {
		this.myFileFileName = myFileFileName;
	}
	public String getMyFileContentType() {
		return myFileContentType;
	}
	public void setMyFileContentType(String myFileContentType) {
		this.myFileContentType = myFileContentType;
	}
	public String getSavePath() {
		return savePath;
	}
	public void setSavePath(String savePath) {
		this.savePath = savePath;
	}
	public String execute() throws Exception{
		String newFileName=null;
		//得到当前时间自1900年1月7日0分0秒开始流逝的毫秒数,将这个毫秒数作为上传文件的文件名
		String now = ""+(1900+new Date().getYear()) + (1+new Date().getMonth())+ (7+new Date().getDay()) + new Date().getHours()+new Date().getMinutes()+new Date().getSeconds() ;
		System.out.print("年"+(1900+new Date().getYear())+"/月"+(1+new Date().getMonth())+"/日"+(7+new Date().getDay())+"/时"+new Date().getHours()+"/分"+new Date().getMinutes()+"/秒"+new Date().getSeconds()) ;
		//得到保存上传文件的目录的真实路径
		String path=ServletActionContext.getServletContext().getRealPath(savePath);
		File dir=new File(path);
		//如果这个目录不存在,则创建它。
		if(!dir.exists()){
			dir.mkdir();
		}
		int index=myFileFileName.lastIndexOf(".");
		//判断上传文件的名是否有扩展名
		if(index!=-1){
		newFileName=now+myFileFileName.substring(index);
		
		}else{
			newFileName=now;
		}
		
		BufferedOutputStream bos=null;
		BufferedInputStream bis=null;
		//读取保存在临时目录下的上传文件,写入到新的文件中。
		FileInputStream fis=new FileInputStream(myFile);
		bis=new BufferedInputStream(fis);
		FileOutputStream fos=new FileOutputStream(new File(dir,newFileName));
		bos=new BufferedOutputStream(fos);
		byte[] buf=new byte[4096];
		int len=-1;
		while((len=bis.read(buf))!=-1){
			bos.write(buf, 0, len);
		}
		bos.flush();
		
		return SUCCESS;
	}
	

}











上传文件类型和名字:

Content-Type not allowed: file "09poC_wallpapers.jpg" "upload_1ea6fe4e_13611ac7d7c__8000_00000012.tmp" image/pjpeg


.a :
application/octet-stream

.ai :
application/postscript

'.aif' :
  'audio/x-aiff',

'.aifc' :
  'audio/x-aiff',

'.aiff' :
  'audio/x-aiff',

'.au' : '
audio/basic',

'.avi' :
'video/x-msvideo',

'.bat' :
'text/plain',

'.bcpio' :
  'application/x-bcpio',

'.bin' :
  'application/octet-stream',

'.bmp' :
  'image/x-ms-bmp',

'.c' :
'text/plain',




# Duplicates



'.cdf' :
  'application/x-cdf',




'.cdf'
: 'application/x-netcdf',

'.cpio' :
'application/x-cpio',

'.csh' :
  'application/x-csh',

'.css' :
  'text/css',

'.dll' :
  'application/octet-stream',

'.doc' :
  'application/msword',

'.dot' :
  'application/msword',

'.dvi' :
  'application/x-dvi',

'.eml' :
  'message/rfc822',

'.eps' :
  'application/postscript',

'.etx' :
  'text/x-setext',

'.exe' :
  'application/octet-stream',

'.gif' :
  'image/gif',

'.gtar' :
'application/x-gtar',

'.h' :
  'text/plain',

'.hdf' :
  'application/x-hdf',

'.htm' :
  'text/html',

'.html' :
  'text/html',

'.ief' :
  'image/ief',

'.jpe' :
  'image/jpeg',

'.jpeg' :
  'image/jpeg',

'.jpg' :
  'image/jpeg',

'.js' :
'application/x-javascript',

'.ksh' :
  'text/plain',

'.latex' :
  'application/x-latex',

'.m1v' :
'video/mpeg',

'.man' :
  'application/x-troff-man',

'.me' :
  'application/x-troff-me',

'.mht' :
  'message/rfc822',

'.mhtml' :
  'message/rfc822',

'.mif' :
  'application/x-mif',

'.mov' :
  'video/quicktime',

'.movie' :
  'video/x-sgi-movie',

'.mp2' :
  'audio/mpeg',

'.mp3' :
  'audio/mpeg',

'.mpa' :
  'video/mpeg',

'.mpe' :
  'video/mpeg',

'.mpeg' :
'video/mpeg',

'.mpg' :
'video/mpeg',

'.ms' :
  'application/x-troff-ms',

'.nc' :
  'application/x-netcdf',

'.nws' :
  'message/rfc822',

'.o' :
'application/octet-stream',

'.obj' :
  'application/octet-stream',

'.oda' :
  'application/oda',

'.p12' :
  'application/x-pkcs12',

'.p7c' :
  'application/pkcs7-mime',

'.pbm' :
  'image/x-portable-bitmap',

'.pdf' :
'application/pdf',

'.pfx' :
  'application/x-pkcs12',

'.pgm' :
  'image/x-portable-graymap',

'.pl' :
  'text/plain',

'.png' :
  'image/png',

'.pnm' :
  'image/x-portable-anymap',

'.pot' :
  'application/vnd.ms-powerpoint',

'.ppa' :
  'application/vnd.ms-powerpoint',

'.ppm' :
  'image/x-portable-pixmap',

'.pps' :
  'application/vnd.ms-powerpoint',

'.ppt' :
  'application/vnd.ms-powerpoint',

'.ps' :
  'application/postscript',

'.pwz' :
  'application/vnd.ms-powerpoint',

'.py' :
  'text/x-python',

'.pyc' :
  'application/x-python-code',

'.pyo' :
  'application/x-python-code',

'.qt' :
  'video/quicktime',

'.ra' :
  'audio/x-pn-realaudio',

'.ram' :
  'application/x-pn-realaudio',

'.ras' :
  'image/x-cmu-raster',

'.rdf' :
  'application/xml',

'.rgb' :
  'image/x-rgb',

'.roff' :
  'application/x-troff',

'.rtx' :
  'text/richtext',

'.sgm' :
  'text/x-sgml',

'.sgml' :
  'text/x-sgml',

'.sh' :
  'application/x-sh',

'.shar' :
  'application/x-shar',

'.snd' :
  'audio/basic',

'.so' :
  'application/octet-stream',

'.src' :
  'application/x-wais-source',

'.sv4cpio':
  'application/x-sv4cpio',

'.sv4crc' :
  'application/x-sv4crc',

'.swf' :
  'application/x-shockwave-flash',

'.t' :
  'application/x-troff',

'.tar' :
  'application/x-tar',

'.tcl' :
  'application/x-tcl',

'.tex' :
  'application/x-tex',

'.texi' :
  'application/x-texinfo',

'.texinfo':
  'application/x-texinfo',

'.tif' :
  'image/tiff',

'.tiff' :
  'image/tiff',

'.tr' :
  'application/x-troff',

'.tsv' :
  'text/tab-separated-values',

'.txt' :
  'text/plain',

'.ustar' :
  'application/x-ustar',

'.vcf' :
  'text/x-vcard',

'.wav' :
  'audio/x-wav',

'.wiz' :
  'application/msword',

'.wsdl' :
  'application/xml',

'.xbm' :
  'image/x-xbitmap',

'.xlb' :
  'application/vnd.ms-excel',




# Duplicates



'.xls' :
  'application/excel',

'.xls' :
  'application/vnd.ms-excel',

.xml :
text/xml

.xpdl:
application/xml

.xpm :
image/x-xpixmap

.xsl :
  application/xml

.xwd :
image/x-xwindowdump

.zip :
application/zip




firefox 和 ie 的文件类型区别



Firefox:
image/jpeg, image/bmp, image/gif, image/png

ie 6:
  image/pjpeg ,image/bmp, image/gif, image/x-png

ie 7:
image/pjpeg, image/bmp, image/gif, image/x-png

ie 8:
image/pjpeg, image/bmp, image/gif, image/x-png

Ie 9: 
image/jpeg, image/bmp, image/gif, image/png




所以在struts.xml配置文件中需要

<param name="allowedTypes">
      image/bmp,image/png,image/gif,image/jpeg,image/jpg,
      image/pjpeg ,image/bmp, image/gif, image/x-png,
</param>

你可能感兴趣的:(struts)