您的位置:首页 > Web前端

我的上传组件-开始干活啦----3上传处理的action和其它方法

2009-11-10 04:41 330 查看
 

upload.action

public class UploadAction extends BaseAction {
private static final long serialVersionUID = -7472787698325813464L;
private File[] upload;
// struts拦截到的上传文件类型,在表单名后加ContentType
private String[] uploadContentType;
// 上传的文件名,在表单名后加FileName
private String[] uploadFileName;
// 上传保存的路径
private String savePath;
private String[] targetFileName;
/************************ method ******************************/
@Override
public String execute() throws Exception {
setTargetFileName(UploadUtil.getUploadPath(upload, savePath,
uploadFileName));
return SUCCESS;
}
/*************************get set ****************************/
}


 

UploadUtil

package com.flexoa.util.file;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import org.apache.struts2.ServletActionContext;
/**
* 写文件(IO操作) 到目标位置 重新命名文件
*
* @author Administrator
*
*/
public class UploadUtil {
private static final int BUFFER_SIZE = 16 * 1024;
/**
* 上传的处理方法,这里还调用了上传的类 返回上传完后,拼装好的路径,数据库只保存路径
*
* @return photo
*/
public static String[] getUploadPath(File[] upload, String savePath,
String[] photosFileName) {
String destPath = ServletActionContext.getServletContext().getRealPath(
savePath);
// System.out.println("  destPath : " + destPath);
// 先检查目录存在否
File filePath = new File(destPath);

// 如果不存在则创建
if (!filePath.exists()) {
filePath.mkdirs();
}
/**
* 先定义转义字符,如果上传的file表单有值,就开始重新命名(有单独的方法),写文件(有单独的方法)到目标位置
*/
String[] targetPath = new String[upload.length];
if (upload != null) {
// 文件保存的路径,并初始长度
String[] myDir = new String[upload.length];
// 文件最终的名字
String[] tName = new String[upload.length];
// 取目标目录
// System.out.println("  savePath : " + savePath);
for (int i = 0; i < upload.length; i++) {
// 生成文件名
tName[i] = UploadUtil.generateFileName(photosFileName[i]);

targetPath[i] = savePath + "/" + tName[i];

// 拼装保存的路径
myDir[i] = destPath + "//" + tName[i];
// 建立目标文件,将文件复制到目录
File destFile = new File(destPath, tName[i]);
UploadUtil.writeFile(upload[i], destFile);
}
}
return targetPath;
}
/**
* 写文件
* @param src
*            传递增来的源文件
* @param goal
*            写入的最终文件
*/
public static void writeFile(File src, File goal) {
InputStream is = null;
OutputStream os = null;
try {
is = new BufferedInputStream(new FileInputStream(src), BUFFER_SIZE);
os = new BufferedOutputStream(new FileOutputStream(goal),
BUFFER_SIZE);
byte[] buffer = new byte[BUFFER_SIZE];
int length = 0;
while ((length = is.read(buffer)) > 0) {
os.write(buffer, 0, length);
}
is.close();
os.close();
} catch (IOException e) {
throw new RuntimeException("文件上传异常!");
}
}
/**
* 构造文件名
*/
public static String generateFileName(String fileName) {
// 按格式取时间
DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd_HHmmss_");
String strDate = dateFormat.format(new Date());
// 在10000里抽奖,生成编号
int random = new Random().nextInt(100000);
// 设置获得文件名后缀的字符
int position = fileName.lastIndexOf(".");
String extension = fileName.substring(position);
// 拼装并返回
return strDate + random + extension;
}
}


 

 

struts-upload.xml

<struts>
<constant name="struts.custom.i18n.resources" value="message"></constant>
<!-- 上传文件最大值 -->
<constant name="struts.multipart.maxSize" value="10240000" />
<package name="uploadGroup" extends="struts-default" namespace="/">
<action name="upload" class="uploadBean">
<result>/include/upload/upload_success.jsp
</result>
<result name="input">/include/upload/upload_success.jsp
</result>
<interceptor-ref name="fileUpload">
<!-- 配置允许上传的文件类型,多个用","分隔 -->
<param name="allowedTypes">
image/bmp,image/png,image/gif,image/jpeg,image/jpg ,image/x-png,
image/pjpeg
</param>
<!-- 配置允许上传的文件大小,单位字节 -->
<param name="maximumSize">10240000</param>
</interceptor-ref>
<!-- 动态设置Action中的savePath属性的值 -->
<param name="savePath">${savePath}</param>
<interceptor-ref name="defaultStack" />
</action>
</package>
</struts>


 

 

 

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息