tapestry的上传提供了很简洁的代码,但很多时候并不是我们想要的,例如上到的本工程目录的文件夹下怎么实现?虽然在服务里面设置了文件上传的大小,但那是所有上传的控制,能不能在某个上传的时候进行文件大小上传限制?等等诸如此类的问题。下面看下源码:
UploadDetail.java
/**
* 项目名称:TapestryStart
* 开发模式:Maven+Tapestry5.x+Tapestry-hibernate+Mysql
* 网址: http://www.flywind.org
* 版本:1.0
* 编写:飞风
* 时间:2012-02-29
*/
package com.tapestry.app.pages;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.services.ApplicationGlobals;
import org.apache.tapestry5.services.RequestGlobals;
import org.apache.tapestry5.upload.services.UploadedFile;
import com.tapestry.app.util.UiUtils;
public class UploadDetail {
@Inject
private ApplicationGlobals applicationGlobals;
@Inject
private RequestGlobals requestGlobals;
@Property
private UploadedFile file;
@Persist
@Property
private String dataUrl;
@Persist
@Property
private String tooBig;
protected final HttpServletRequest getRequest() {
return requestGlobals.getHTTPServletRequest();
}
void onSuccess(){
uploadImage();
}
void uploadImage(){
// 文件保存目录路径
String savePath = applicationGlobals.getServletContext().getRealPath("/uploadImages/") + "\\";
// 文件保存目录URl地址
String saveUrl = getRequest().getContextPath() + "/uploadImages/";
//最大文件大小
long maxSize = 1048576;
// 检查目录
File uploadDir = new File(savePath);
if (!uploadDir.exists()) {
uploadDir.mkdirs();
}
// 检查目录写权限
if (!uploadDir.canWrite()) {
System.out.println("上传目录没有写权限。");
return;
}
//文件夹名
String dirName = "uploadData";
// 创建文件夹
savePath += dirName + "/";
saveUrl += dirName + "/";
File saveDirFile = new File(savePath);
if (!saveDirFile.exists()) {
saveDirFile.mkdirs();
}
String fileName = file.getFileName();
//转换文件名为时间_随机数的名字格式如21120229_8888745544.gif
SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
String newFileName = df.format(new Date()) + "_" + UiUtils.createFileName(fileName);
File copied = new File(savePath + newFileName);
long fileSize = file.getSize();
//判断文件大小
if(fileSize > maxSize){
tooBig = "上传失败!文件大小超过1M。";
System.out.println("上传失败!文件大小超过1M。");
return;
}else{
tooBig = "";
}
file.write(copied);
//图片上传成功之后返回的途径
dataUrl = saveUrl + newFileName;
}
}
UploadDetail.tml
<html t:type="layout" title="tapestryStart Index" t:sidebarTitle="Framework Version"
xmlns:t="http://tapestry.apache.org/schema/tapestry_5_3.xsd" xmlns:p="tapestry:parameter">
文件路径:${dataUrl}<br/>
<t:form t:id="uploadForm">
<t:errors/>
<input t:type="upload" t:id="file" t:value="file" validate="required"/><span style="color:red">${tooBig}</span>
<br/>
<input type="submit" value="上传"/>
</t:form>
</html>
UiUtils.java
package com.tapestry.app.util;
import java.util.Date;
import java.util.Random;
public class UiUtils {
public static String splitString(String str, String split) {
int index = str.lastIndexOf(split);
return str.substring(index + 1);
}
public static String splitStringPath(String str) {
str = str.replace("/", "\\");
int index = str.lastIndexOf("\\");
return str.substring(index + 1);
}
public static String createFileName(String name) {
Long randomNum = new Random().nextLong();
String radomStr = randomNum.toString().substring(2, 5);
String accessoryAutoName = new Date().getTime() + radomStr;
String extendedName = splitString(name, ".");
String fileName = accessoryAutoName;
if (extendedName != null) {
fileName = accessoryAutoName + "." + extendedName;
}
return fileName;
}
}