研究了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
分享到:
相关推荐
使用struts2 上传图片到项目webapps目录下 然后生成缩略图 返回到页面显示缩略图
ckeditor和struts2实现图片上传和编辑器上浏览!
实现图片的上传和图片的预览,主要用strust2实现的
struts2 ajax图片上传
struts2上传图片到Oracle,并进行显示,测试可以成功上传
ssh框架用struts2 hibernate实现图片的上传源码
Struts2上传图片 简单的图片上传功能!
Struts2+Jquery 同步上传图片
使用struts2 上传图片,上传图片后生成一张压缩后的图片,和生成一张小图片,在网页上不用显示大图片,显示的缩小后的图片
使用struts2框架实现图片上传,不需要js开发
可直接运行,包含jar文件,struts2 图片上传,图片大小,图片格式的限制,客户端文件类型判断等
struts2 批量上传 图片+ 文件 显示进度条
struts实现图片上传功能。包含详细的代码和配置信息。
struts2 上传图片限制.struts2 上传图片限制.struts2 上传图片限制.
Struts2上传图片并生成等比例缩略图的实例,在上传文件的同时生成等比例的缩略图,分放在不同的文件夹下。自己亲测,没有问题。
struts2 文件上传 struts2上传标签file fileuploadstruts2 文件上传 struts2上传标签file fileuploadstruts2 文件上传 struts2上传标签file fileupload
在struts2基础上实现的包括单文件、多文件的上传及下载
struts2.0图片上传,文件的配置。struts2。0的学习。io的技术。
使用struts框架实现图片上传功能和显示图片。使用struts框架实现图片上传功能和显示图片。