Home > Article > Backend Development > 高分高手翻译一PHP文件上传代码
高分求助高手翻译一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);