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>
<?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>
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;}}<%@ 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>
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;}}