Struts2文件上传 ,也可当做普通文件上传封装,,发下交流

Struts2文件上传,Struts2文件上传 文件大小限制,文件类型过滤

package com.webs.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Calendar;
import java.util.Random;

public class FileUtilFinal {
private String allowType = “gif#jpg#txt#doc”;
private String error=null;
private String extName=null;
private String fileName=null;
private Integer fileSize=2097152;
private File upload=null;
private String filePath=null;
private String uploadContentType=null;
private String uploadFileName=null;

public FileUtilFinal() {
super();
}

public FileUtilFinal(File file) {
this.upload = file;
}

public FileUtilFinal(File file, String uploadFileName) {
this.upload = file;
this.uploadFileName = uploadFileName;
}

public FileUtilFinal(File file, String uploadFileName, String filePath) {
this.upload = file;
this.uploadFileName = uploadFileName;
this.filePath = filePath;
}
public FileUtilFinal(File file, String uploadFileName,String uploadContentType, String filePath) {
this.upload = file;
this.uploadFileName = uploadFileName;
this.uploadContentType=uploadContentType;
this.filePath = filePath;
}
public boolean delete(String path){
File temp=new File(path);
if(!temp.exists()){
this.setError(“file dosn’s exits!”);
return false;
}else{
try{
if(temp.delete()){
this.setError(“file deleted successfully!”);
return true;
}else{
this.setError(“file delete failed!”);
return false;
}
}catch(Exception e){
this.setError(e.getMessage());
return false;
}
}
}
public boolean uploading() {
if (this.getUpload() == null || this.getUploadContentType() == null
|| this.getUploadContentType().equals(“”))
return false;
File dir=new File(this.getFilePath());
if(!dir.exists())
this.createNewDir(dir);
else{
File temp=new File(dir.getAbsolutePath()+”//”+this.getDestFileName()+this.getExtName());
if(temp.exists()){
this.setError(“file already exits!”);//文件已经存在
return false;
}else{
if(!this.createNewFile(temp))//文件创建失败则返回
return false;
if(isOverBig()){
temp.delete();
return false;
}
if(!isAllow()){
temp.delete();
return false;
}
if(this.write(temp, upload)){
return true;
}else{
return false;
}

}

}
return true;
}

private String getDestFileName() {
Calendar time = Calendar.getInstance();
Random rd = new Random();
String fileName = String.valueOf(time.get(Calendar.YEAR))
+ String.valueOf(time.get(Calendar.MONTH) + 1)
+ String.valueOf(time.get(Calendar.DAY_OF_MONTH))
+ String.valueOf(time.get(Calendar.HOUR_OF_DAY))
+ String.valueOf(time.get(Calendar.MINUTE))
+ String.valueOf(time.get(Calendar.SECOND))
+ String.valueOf(rd.nextInt(100));
return fileName;
}

private boolean createNewFile(File file) {
try {
if (file.createNewFile())
return true;
else
return false;
} catch (Exception e) {
this.setError(e.getMessage());
return false;
}
}
private boolean createNewDir(File file){
try {
if (file.mkdir())
return true;
else
return false;
} catch (Exception e) {
this.setError(e.getMessage());
return false;
}
}
private boolean isOverBig() {
if (this.upload.length() > this.fileSize)
return true;
this.setError(“File is too big! max fileSize is can +”+this.getFileSize()/1024/1024+”M!”);
return false;
}
private boolean isAllow() {
String ext=this.getUploadFileName().substring(this.uploadFileName.lastIndexOf(“.”)+1);
if(this.allowType.indexOf(ext)>-1){
return true;
}
this.setError(“FileType misMatch!You can only upload “+this.getAllowType());
return false;
}
private boolean write(File dest, File orgin) {
try {
FileOutputStream fos = new FileOutputStream(dest);
FileInputStream fis = new FileInputStream(orgin);
byte[] buff = new byte[1024];
int len = 0;
while ((len = fis.read(buff)) > 0) {
fos.write(buff, 0, len);
}
return true;
} catch (Exception e) {
this.setError(e.getMessage());
return false;
}
}

public String getAllowType() {
return allowType;
}

public void setAllowType(String allowType) {
this.allowType = allowType;
}

public String getError() {
return error;
}

public void setError(String error) {
this.error = error;
}

public String getExtName() {
return this.getUploadFileName().substring(this.uploadFileName.lastIndexOf(“.”));
}

public void setExtName(String extName) {
this.extName = extName;
}

public String getFileName() {
return fileName;
}

public void setFileName(String fileName) {
this.fileName = fileName;
}

public Integer getFileSize() {
return fileSize;
}

public void setFileSize(Integer fileSize) {
this.fileSize = fileSize;
}

public File getUpload() {
return upload;
}

public void setUpload(File upload) {
this.upload = upload;
}

public String getFilePath() {
return filePath;
}

public void setFilePath(String filePath) {
this.filePath = filePath;
}

public String getUploadContentType() {
return uploadContentType;
}

public void setUploadContentType(String uploadContentType) {
this.uploadContentType = uploadContentType;
}

public String getUploadFileName() {
return uploadFileName;
}

public void setUploadFileName(String uploadFileName) {
this.uploadFileName = uploadFileName;
}

}
STUTS2 ACTION 里的具体 代码,,,文件格式和文件大小限制都在配置文件里给出…

配置文件内容

<action name=”upload” class=”com.webs.action.file.FileUtilAction” method=”upload”>
<param name=”allowType”>gif#jpg#doc#txt</param>
<param name=”fileSize”>2097152</param><!– 最大可上传2M大的文件 –>
<result name=”ok”>list.jsp</result>
</action>

ACTION 代码

package com.webs.action.file;

import java.io.File;

import org.apache.struts2.ServletActionContext;

import com.webs.util.FileUtilFinal;

public class FileUtilAction {
File upload;
String uploadContentType;
String uploadFileName;
String allowType;
String fileSize;
public String getAllowType() {
return allowType;
}
public void setAllowType(String allowType) {
this.allowType = allowType;
}
public String getFileSize() {
return fileSize;
}
public void setFileSize(String fileSize) {
this.fileSize = fileSize;
}
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 getUploadFileName() {
return uploadFileName;
}
public void setUploadFileName(String uploadFileName) {
this.uploadFileName = uploadFileName;
}
public String upload(){
String path=ServletActionContext.getRequest().getRealPath(“uploads”);
FileUtilFinal fu=new FileUtilFinal(upload,uploadFileName,uploadContentType,path);
if(fu.uploading())
ServletActionContext.getRequest().setAttribute(“message”,”上传成功!”);
else
ServletActionContext.getRequest().setAttribute(“message”,fu.getError());
return “ok”;
}
}

页面文件内容

<form action=”upload.action” method=”post”
enctype=”multipart/form-data”>
<table>
<tr>
<td id=”more”>
<br>
<input type=”file” name=”upload”>
<input type=submit value=”upload”>
<br />
<s:property value=”#attr.message” escape=”false”/>
<s:actionerror />
<s:actionmessage />
</td>
</tr>
</table>
</form>
好建议请留下,,,谢谢合作.

发表评论

电子邮件地址不会被公开。 必填项已用 * 标注

您可以使用这些 HTML 标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>