这是一段保存图片文件的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, "\ ", "\\ "))
%>
------解决方案--------------------
对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();
.............
------解决方案--------------------
好像你是在问 jsp另存图片是不?
try {
String path= "upload/ ";
SmartUpload uploader=new SmartUpload();
uploader.initialize(pageContext);//初始化
uploader.setMaxFileSize(302400000);