把asp改成jsp

yc_zhj 2007-08-10 10:26:06
这是一段保存图片文件的asp代码,那位大大,能改成jsp执行的代码吗
<%
Response.Expires = -1
Response.AddHeader "Pragma", "no-cache"
Response.AddHeader "Cache-Control", "no-cache, must-revalidate"
%>
<%

Function GetRndFileName()
Dim tmpstr
randomize
tmpstr=Int(1000*rnd)
tmpstr=year(now)&month(now)&day(now)&hour(now)&minute(now)&second(now)&tmpstr
GetRndFileName=tmpstr
End Function

path="pictemp\"&getrndfilename()&".png"

TotalBytes = Request.TotalBytes
Set bSourceData = server.createobject("ADODB.Stream")
bSourceData.Open
bSourceData.Type = 1
biData = Request.BinaryRead(TotalBytes)
bSourceData.Write biData
bSourceData.SaveToFile (server.mappath(path))
set bsourcedata=nothing
response.write("fileurl="&replace(path,"\","\\"))



%>
...全文
248 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
dengdai520 2007-08-10
  • 打赏
  • 举报
回复
好像你是在问 jsp另存图片是不?

try {
String path="upload/";
SmartUpload uploader=new SmartUpload();
uploader.initialize(pageContext);//初始化
uploader.setMaxFileSize(302400000);
uploader.setAllowedFilesList("jpg,gif,JPG,GIF,mp3,MP3");
uploader.upload();

String fileName = uploader.getFiles().getFile(0).getFileName(); ////获取文件名
out.println(fileName);
String fileExt = uploader.getFiles().getFile(0).getFileExt(); ////获取文件名

out.println(fileExt);
path += "aa"+"."+fileExt;//完整文件名
out.println("<img width='50' height='50' src="+path+">");

uploader.getFiles().getFile(0).saveAs(path);//保存在容器目录下

out.print("<script>alert('上传成功');</script>");
out.print("上传成功");

} catch (Exception e) {
log(e.getMessage());
out.print("上传失败");
}
swoky 2007-08-10
  • 打赏
  • 举报
回复
对ASP不是很懂,看了一下你的代码,好像是想将数据输出到一个随机的文件中,但是我不清楚你的那个数据是从哪里来的,我写了个从数据库中读取的。

以下为JAVA代码,希望对你有帮助
---------------------------------
/*产生一个跟随日期的文件名,可能和你的那个有点差异*/
public String randomFileName(){

int cnt = 0;

java.util.Date dt = new java.util.Date(System.currentTimeMillis());

java.text.SimpleDateFormat fmt =

new java.text.SimpleDateFormat("yyyyMMddHHmmssSSS");

String fileName = fmt.format(dt);

cnt = (cnt+1) % 100;

StringBuffer sBuf = new StringBuffer("0000").append(cnt);

sBuf.delete(0,sBuf.length()-2); //使产生的文件名的长度一致

fileName = fileName + "_" + sBuf.toString();

return fileName;
}

String path = "pictemp\\" + randomFileName() + ".png";

java.io.OutputStream outword = new java.io.FileOutputStream( path );

//下面为连接数据库的代码
String Dsn = ..
.......
.....
java.sql.Connection connect = DriverManager.getConnection(Dsn,UserName,PassWord);

java.sql.Statement state = connect.createStatement();

ResultSet Rs = state.executeQuery("select content from filedata);

if( Rs.next() ){
java.io.InputStream inword = Rs.getBinaryStream(1);

if( inword != null){

byte[] buf = new byte[2048];

while( true ){

int nread = inword.read( buf );

if( nread != -1 )

outword.write( buf , 0 , nread );

else break;
}
}
inword.close();
}
outword.flush();
outword.close();
Rs.close();
state.close();
.............

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧