首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

struts2 下传图片改变原始图片名称的方法

2012-10-25 
struts2 上传图片改变原始图片名称的方法!研究了2天,终于利用STRUTS2 自带的唯一不重名的方法 实现了修改

struts2 上传图片改变原始图片名称的方法!
  研究了2天,终于利用STRUTS2 自带的唯一不重名的方法 实现了修改上传文件名的方法。现在和大家分享一下~~!
   首先加入相应的JAR 包,我就不用多说了吧!
先从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">  <welcome-file-list>    <welcome-file>index.jsp</welcome-file>  </welcome-file-list>  <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></web-app>


2, 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.objectFactory" value="spring"/>--><constant name="struts.i18n.encoding" value="GBK"/><package name="upload1" extends="struts-default">  <action name="upload" method="upload" >  <interceptor-ref name="fileUpload">        <param name ="allowedTypes">                     image/bmp,image/PNG,image/gif,image/JPEG,image/jpg,image/pjpeg                </param>                 <param name="maximumSize">102400000</param>         </interceptor-ref>         <interceptor-ref name="defaultStack"/>           <param name="savePath">/photo</param>       <result name="success" >success.jsp</result>       <result name="input"> /index.jsp</result>          </action></package></struts>


下面是Upload.java
package org.example.action;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionSupport;public class Upload extends ActionSupport {/** *  */private static final long serialVersionUID = -8204063374280945416L;private File upload;private String uploadFileName;private String uploadContentType;private String savePath;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 getSavePath() {      return ServletActionContext.getRequest().getRealPath(savePath);}public void setSavePath(String savePath) {this.savePath = savePath;}public String upload() throws Exception{//得到上传文件的后缀名String b=getUploadContentType();String houzhui=b.substring(b.indexOf("/")+1, b.length());houzhui=(houzhui.equals("pjpeg")?"jpg":houzhui);//得到文件的新名字String a = upload.getName();    String mingzi= a.substring( 0, a.length()-3 ) + houzhui;  // 创建文件  FileOutputStream fos = new FileOutputStream(getSavePath() + "\" + mingzi);  FileInputStream fis = new FileInputStream(getUpload());  byte[] buffer = new byte[1024];  int len = 0;  while ((len = fis.read(buffer)) > 0)  {fos.write(buffer , 0 , len); }    return SUCCESS;}public String getUploadFileName() {return uploadFileName;}public void setUploadFileName(String uploadFileName) {this.uploadFileName = uploadFileName;}}


3,下面是index.jsp文件的源码
<%@ page language="java"  pageEncoding="utf-8"%><%@ taglib prefix="s" uri="/struts-tags" %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>         <title>My JSP 'index.jsp' starting page</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0">    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">-->  </head>  <s:fielderror></s:fielderror>  <s:form method="post" action="upload" enctype="multipart/form-data">  <s:textfield name="title" label="title"></s:textfield>  <s:file name="upload" label="file"></s:file>  <s:submit></s:submit>  </s:form>  <body>  </body></html>



备注:
   也可以通过调用JS 的方法先得到要上传文件的后缀名,这种方法,大家自由发挥吧,想研究的朋友请联系我,QQ:296502215




<%@ page language="java" pageEncoding="utf-8"%><%@ taglib prefix="s" uri="/struts-tags" %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head><script> function ty(){var srcc = document.form.upload.value;srcc = srcc.toString()start = srcc.indexOf(".");end = srcc.length;ts=srcc.substring(start+1,end);nts=ts.toLowerCase();document.form.houzhui.value=nts;}</script> <title>My JSP 'index.jsp' starting page</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">--> </head> <s:fielderror></s:fielderror> <s:form method="post" action="upload" enctype="multipart/form-data" onsubmit="return ty();" name="form" > <s:textfield name="title" label="title"></s:textfield> <s:file name="upload" label="file"></s:file> <s:hidden name="houzhui"></s:hidden> <s:submit></s:submit> </s:form> <body> </body></html>

2,下面是action 的代码
package org.example.action;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionSupport;public class FileUpload extends ActionSupport {/** *  */private static final long serialVersionUID = -8204063374280945416L;private File upload;private String uploadFileName;private String uploadContentType;private String savePath;private String houzhui;public String getHouzhui() {return houzhui;}public void setHouzhui(String houzhui) {this.houzhui = houzhui;}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 getSavePath() {      return ServletActionContext.getRequest().getRealPath(savePath);}public void setSavePath(String savePath) {this.savePath = savePath;}public String upload() throws Exception{//得到随机的新文件名String a=upload.getName();String b=a.substring(0, a.length()-3);//新文件名+得到的后缀String mingzi=b+ getHouzhui();  FileOutputStream fos = new FileOutputStream(getSavePath() + "\" + mingzi);  FileInputStream fis = new FileInputStream(getUpload());  byte[] buffer = new byte[1024];  int len = 0;  while ((len = fis.read(buffer)) > 0)  {fos.write(buffer , 0 , len); }    return SUCCESS;}public String getUploadFileName() {return uploadFileName;}public void setUploadFileName(String uploadFileName) {this.uploadFileName = uploadFileName;}}


备注,其他的配置和 上边的文章的配置一样,比如:web.xml, struts.xml等
2 楼 yufeng217 2009-02-10   牛人,厉害啊 3 楼 glagra 2009-03-22   为什么一定要把当前时间加在后辍名前呢..不能直接加在名字最前面吗??

热点排行