在JSP页面中调用Java程序

时间:2014-01-04 09:38:47

标签: java jsp

我是java的新手。我正在尝试在jsp页面上调用java函数。该函数应该将文件从一个目录复制到另一个目录。通过Jsp我必须浏览ahd选择一个文件..在此之后,我必须单击运行按钮然后它应该执行我在java程序中给出的操作。如何在jsp调用这个java程序..请帮帮我.. Java代码:

package copy;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class CopyDirectoryExample {
    public static void main(String[] args) {    
        File srcFolder = new File("D:\\CETAF");
        File destFolder = new File("C:\\ExecFolder");

         //make sure source exists
        if(!srcFolder.exists()){
            System.out.println("Directory does not exist.");
            //just exit
            System.exit(0);
        } else {
            try {
                copyFolder(srcFolder,destFolder);
            } catch(IOException e) {
                e.printStackTrace();
                //error, just exit
                System.exit(0);
           }
        }

        System.out.println("Done");
    }

    public static void copyFolder(File src, File dest) throws IOException {
        if(src.isDirectory()) {

            //if directory not exists, create it
            if(!dest.exists()){
               dest.mkdir();
               System.out.println("Directory copied from " 
                              + src + "  to " + dest);
            }

            //list all the directory contents
            String files[] = src.list();

            for (String file : files) {
               //construct the src and dest file structure
               File srcFile = new File(src, file);
               File destFile = new File(dest, file);
               //recursive copy
               copyFolder(srcFile,destFile);
            }
        } else {
            //if file, then copy it
            //Use bytes stream to support all file types
            InputStream in = new FileInputStream(src);
            OutputStream out = new FileOutputStream(dest); 
            byte[] buffer = new byte[1024];
            int length;
            //copy the file content in bytes 
            while ((length = in.read(buffer)) > 0){
                out.write(buffer, 0, length);
            }
            in.close();
            out.close();
            System.out.println("File copied from " + src + " to " + dest);
        }
    }
}

的index.jsp

 <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01                       Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
       <HTML><HEAD><TITLE>Display file upload form to the user</TITLE> 
</HEAD>
<BODY>
 <FORM ENCTYPE="multipart/form-data" ACTION="upload.jsp" METHOD=POST>
<br>
<br>
<br>
          <center>
                <table border="0" >     
                <tr>         
           <center>            
           <td colspan="2" align="center"><B>UPLOAD THE FILE</B>  
          <center>          
            </td>      
            </tr>      
            <tr>           
             <td colspan="2" align="center"></td>
                 </tr>     
             <tr>          
          <td><b>Choose the file To Upload:</b></td>          
            <td><INPUT NAME="file" TYPE="file"></td>
                  </tr>    
                 <tr>           
              <td colspan="2" align="center"></td>
                 </tr>     
               <tr>           
                   <td colspan="2" align="center"><input type="submit"
              value="RUN"></td>
                  </tr>    
                  </table>       
                          </center>       
                       </FORM>
                       </BODY>
              </HTML>

Upload.jsp

                  <%@ page import="java.io.*"%>
                 <%      String saveFile = "";   
                        String contentType = request.getContentType();  
                  if ((contentType != null) && (contentType.indexOf
                        ("multipart/form-data") >= 0)) {            
               DataInputStream in = new DataInputStream(request.getInputStream());     
                 int formDataLength = request.getContentLength();        
                    byte dataBytes[] = new byte[formDataLength];       
                         int byteRead = 0;        
                    int totalBytesRead = 0;       
                while (totalBytesRead < formDataLength) {       
                 byteRead = in.read(dataBytes, totalBytesRead, formDataLength);            
              totalBytesRead += byteRead;           
                  }           
                   String file = new String(dataBytes);        
                         saveFile = file.substring(file.indexOf("filename=\"") + 10);       
                           saveFile = saveFile.substring(0, saveFile.indexOf           ("\n"));           
                     saveFile = saveFile.substring(saveFile.lastIndexOf("\\")                           +1,saveFile.indexOf                    ("\""));       
                int lastIndex = contentType.lastIndexOf("=");           
                    String boundary = contentType.substring(lastIndex+ 1,                         contentType.length());          
                      int pos;           
                   pos = file.indexOf("filename=\"");           
             pos = file.indexOf("\n", pos) + 1;         
               pos = file.indexOf("\n", pos) + 1;          
                  pos = file.indexOf("\n", pos) + 1;          
            int boundaryLocation = file.indexOf(boundary, pos) - 4;     
               int startPos = ((file.substring(0, pos)).getBytes()).length;          
             int endPos = ((file.substring(0, boundaryLocation)).getBytes()).length;          
               saveFile = "D:/CETAF/Configuration/" + saveFile;         
                   File ff = new File(saveFile);          
                FileOutputStream fileOut = new FileOutputStream(ff);          
                    fileOut.write(dataBytes, startPos, (endPos - startPos));           
          fileOut.flush();            
                            fileOut.close();%><Br><table border="2">
                <tr>         
                         <td><b>You have successfully upload the file by the name of:</b>         
                          <%                  
                       out.println(saveFile);                 
                     }        
                              %>          
                          </td>      
                          </tr>
                         </table>

以上两个是我的jsp文件...如果我点击运行按钮,它应该调用那个java文件并执行dat action..how来调用那个java文件??

0 个答案:

没有答案