高分求助高手翻译一php文件上传代码
这个代码翻译成php的:
3. 在web端编写上传文件处理文件如上面的index.jsp
使用jspsmart处理代码如下
boolean isok = false;
//程序初始化--------------------------------------------------------------
string path_tmp = request.getrealpath(/) + upload;
string filename_p = texthandle.getdirectory();
string path_new = request.getrealpath(/) + upload\\ + filename_p;
//文件上传----------------------------------------------------------------
smartupload su = new smartupload();
su.initialize(pagecontext);
su.upload();
int count = su.save(path_tmp);
string file_name = ,file_ext,newfilename = 无;
isok=true;
if(count>0){
com.jspsmart.upload.file file = su.getfiles().getfile(0);
if (!file.ismissing()){ //有上传的文件------------------------
file_name = file.getfilename().tolowercase();
if(file.getsize() == 0){ //如果传来的是空文件则删除------------
java.io.file filename1=new java.io.file(path_tmp,file_name);
if(filename1.exists()) filename1.delete();
isok=false;
}
else{ //看是否是正确的文件------------------
file_ext = file_name.substring(file_name.lastindexof(.)+1,file_name.length());
if(file_ext.equals(jpg)||file_ext.equals(gif)||file_ext.equals(jpeg)){
string number = string.valueof( (new java.util.date()).gettime() );
newfilename = filename_p + number + . + file_ext;
//将文件换名保存------------------------------------------
//首先看目录是否存在,如果不存在则创建目录
java.io.file dirop = new java.io.file(path_new);
if(!dirop.exists()) dirop.mkdir();
string path1 = path_tmp + \\ + file_name;
string path2 = path_new + \\ + newfilename;
java.io.file fromfile = new java.io.file(path1);
java.io.file tofile = new java.io.file(path2);
java.io.fileinputstream fis = null;
java.io.fileoutputstream fos = null;
try{
fis = new java.io.fileinputstream(fromfile);
fos = new java.io.fileoutputstream(tofile);
int bytesread;
byte[] buf = new byte[4 * 1024]; // 4k buffer ---------
while ((bytesread = fis.read(buf)) != -1) fos.write(buf, 0, bytesread);
fos.close(); fis.close();
}
catch(java.io.ioexception e){
system.out.println(e);
