上传的工具类

[color=blue]html:
<%@ page language="java" pageEncoding="gb2312"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>

<html>
<head>
<title>JSP for UploadForm form</title>
</head>
<body>
<html:form action="/upload" method="post" enctype="multipart/form-data">
上传 <html:file property="file"/>
<html:submit value="提交"/>
<html:reset value="重置"/>
</html:form>
</body>
</html>

/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.snow.struts.form;

import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;

/**
* MyEclipse Struts
* Creation date: 11-26-2009
*
* XDoclet definition:
* @struts.form name="uploadForm"
*/
public class UploadForm extends ActionForm {
private FormFile file;

public FormFile getFile() {
return file;
}

public void setFile(FormFile file) {
this.file = file;
}
}


package com.snow.util;

import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.upload.FormFile;

import java.util.Iterator;
import java.util.LinkedList;
import java.io.File;
import java.io.FileOutputStream;

public class UploadFile {
// 文件上传对象
private FormFile file = null;
// 提示信息
private String Message = "";
// 后缀名集合
private LinkedList<String> list = null;
// 文件最大值
private String filesize = "50000";
// 路径
private String dirPath = null;
// 文件名
private String fileName=null;
private ActionMessages errors;

public UploadFile() {
this.list= new LinkedList<String>();
this.list.add(".jpg");
this.list.add(".gif");
this.list.add(".png");
this.list.add(".doc");
}

public FormFile getFile() {
return file;
}

public void setFile(FormFile file) {
this.file = file;
}

public String getMessage() {
return Message;
}

public void setMessage(String message) {
Message = message;
}

/* 得到文件的后缀名 */
public String getFilesuffix(String fileName) {
return fileName.substring(fileName.lastIndexOf("."));
}

public LinkedList<String> getList() {
return list;
}

public void setList(LinkedList<String> list) {
this.list = list;
}

/* 验证后缀名 如果有表示是为真,否则为假 */
public boolean checkSuffix(String fix) {
Iterator iterator = list.iterator();
this.list.size();
boolean result = false;
while (iterator.hasNext()) {
String value = (String) iterator.next();
if (value.equals(fix) || value == fix) {
result = true;
return result;
}
}

return result;
}

/* 文件大小的验证 文件的实际大小 小于等于最大值为真 否则为假*/
public boolean checkFileSize() {
int fileSize = file.getFileSize();
int fileSizeCheck = Integer.parseInt(this.filesize);
boolean result = false;
if (fileSize <= fileSizeCheck) {
result = true;
}
return result;
}

public String getFilesize() {
return filesize;
}

public void setFilesize(String filesize) {
this.filesize = filesize;
}

public String getDirPath() {
return dirPath;
}

public void setDirPath(String dirPath) {
this.dirPath = dirPath;
}

public String getFileNamenull() {
return fileName;
}

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

/* 上传的真证方法 */
public void Upload() throws Exception{
String suffix = this.getFilesuffix(this.file.getFileName());

//验证后缀名是否证确
if (!this.checkSuffix(suffix)) {
this.errors = new ActionMessages();
this.errors.add("admin.nofand", new ActionMessage("admin.nofand"));
throw new Exception();
}

//文件大小是否正确
if(!this.checkFileSize()){
this.errors = new ActionMessages();
this.errors.add("admin.nofand", new ActionMessage("admin.nofand"));
throw new Exception();
}

if(this.fileName==null || this.fileName.equals("")){
this.fileName= new java.util.Date().toString().replace(" ", "").replace(":", "")+suffix;
}

String filepath = new String(this.dirPath);
this.CreateDir(filepath);
File f= new File(this.dirPath+"\\"+this.fileName);
FileOutputStream stream= new FileOutputStream(f);
stream.write(file.getFileData());
stream.close();

}
/*无限级创建路径*/
public void CreateDir(String path){
String pathValue= "";
for(int i=0;i<path.length();i++){
pathValue=path.substring(0,i+1);
String lin=path.substring(i,i+1);
if(lin.equals("\\")){
System.out.println(pathValue);
File f= new File(pathValue);
if(!f.exists()){
f.mkdir();
}
}else{
if((i+1)==path.length()){
System.out.println(path);
File f= new File(path);
if(!f.exists()){
f.mkdir();
}
}
}
}
}

public ActionMessages getErrors() {
return errors;
}
}[/color]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值