High score Master Translator a php file upload code

Source: Internet
Author: User
Tags php file upload
High score help Master translator a php file upload code
This code is translated into PHP:

3. Write the upload file processing file on the Web side as above index.jsp

Use Jspsmart to process the code as follows

<%@ page import= "Com.jspsmart.upload.*,common. Texthandle "%><%
Boolean IsOK = false;

Program Initialization--------------------------------------------------------------
String path_tmp = Request.getrealpath ("/") + "Upload";
String filename_p = Texthandle.getdirectory ();
String path_new = Request.getrealpath ("/") + "upload\\" + filename_p;

File Upload----------------------------------------------------------------
Smartupload su = new Smartupload ();
Su.initialize (PageContext);
Su.upload ();
int count = Su.save (path_tmp);

String file_name = "", File_ext,newfilename = "none";

Isok=true;

if (count>0) {
Com.jspsmart.upload.File File = Su.getfiles (). GetFile (0);
if (!file.ismissing ()) {//have uploaded file------------------------
file_name = File.getfilename (). toLowerCase ();

if (file.getsize () = = 0) {//If an empty file is coming, delete the------------
Java.io.File filename1=new Java.io.File (path_tmp,file_name);
if (filename1.exists ()) Filename1.delete ();
Isok=false;
}
else{//See if it is the correct file------------------
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;
Save the file name------------------------------------------
First look at whether the directory exists or create a directory if it does not exist
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);
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.