jsp技巧大全
[08-23 22:09:45] 来源:http://www.duoxue8.com JSP教程 阅读:128次if(s == null)
return new String(abyte0, 0, ai[0]);
return new String(abyte0, 0, ai[0], s);
obj;
return null;
}
}
JSP页:
<%@page contentType="text/html;charset=gb2312" import="package.upload"%>
<%
String Dir = "c:dirupload";
String fn="";
upload upload = new upload();
upload.setUploadDirectory(Dir);
fn=upload.uploadFile(request);
%>
随机图片名称
<%
mySmartUpload.initialize(pageContext);
mySmartUpload.service(request,response);
mySmartUpload.upload();
String fn=mySmartUpload.getFiles().getFile(0).getFileName();
mySmartUpload.save("upload/"); //文件保存的目录为upload
out.println("已经成功上传了文件,请查看<a href=upload/"+fn+">这里</a>");
%>
上面的程序可以上传图片,不过只能上传gif或者JPG图片。
而且保存图片在upload文件夹下面,要想GIF或Jpg图片的名称变为年+月+日+随机数.gif或年+月+日+随机数.jpg
只允许上传jpg或gif图片,在客户端用javaScript控制要好些。
变图片名称可用如下代码:自己看看就明白了。:
//得到实际路径
String realPath = this.masRequest.getRequest().getRealPath("/");
String userPhotoPath = realPath + "images\UserPhoto\";
userPhotoPath = MasString.replace(userPhotoPath,"\","\\");
if (!file.getFileName().trim().equals(""))
{
//根据系统时间生成文件名
Date nowTime = new Date();
emp_Photo = userPhotoPath + String.valueOf(nowTime.getTime()) +"."+ file.getFileExt();
file.saveAs(emp_Photo);
System.out.println("file.saveAs() = " + "OK!!!");
}
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/darasion/archive/2004/07/24/50385.aspx
上一页 [1] [2] [3] [4] [5] [6] [7] [8] [9] [10]
jsp技巧大全 结束。