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" class="org.example.action.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
为什么一定要把当前时间加在后辍名前呢..不能直接加在名字最前面吗??
牛人,厉害啊
下面用调JS 的方法得到上传的后缀名: 1, 以下是 index.jsp 的代码, 在顶部加了 JS代码。
2,下面是action 的代码
备注,其他的配置和 上边的文章的配置一样,比如:web.xml, struts.xml等