You are viewing a plain text version of this content. The canonical link for it is here.
Posted to axis-cvs@ws.apache.org by de...@apache.org on 2005/02/23 11:02:56 UTC

svn commit: r154996 - webservices/axis/trunk/java/modules/core/src/html/upload.jsp

Author: deepal
Date: Wed Feb 23 02:02:55 2005
New Revision: 154996

URL: http://svn.apache.org/viewcvs?view=rev&rev=154996
Log:
Added the commons file upload component and fixed the linux/Windows upload bug

Modified:
    webservices/axis/trunk/java/modules/core/src/html/upload.jsp

Modified: webservices/axis/trunk/java/modules/core/src/html/upload.jsp
URL: http://svn.apache.org/viewcvs/webservices/axis/trunk/java/modules/core/src/html/upload.jsp?view=diff&r1=154995&r2=154996
==============================================================================
--- webservices/axis/trunk/java/modules/core/src/html/upload.jsp (original)
+++ webservices/axis/trunk/java/modules/core/src/html/upload.jsp Wed Feb 23 02:02:55 2005
@@ -4,33 +4,37 @@
                  java.io.Writer,
                  java.io.PrintWriter,
                  java.io.IOException,
-                 org.apache.axis.deployment.DeploymentConstants"%>
+                 org.apache.commons.fileupload.FileUpload,
+                 java.util.List,
+                 org.apache.commons.fileupload.DiskFileUpload,
+                 org.apache.commons.fileupload.FileItem,
+                 java.util.Iterator"%>
 <%@ page contentType="text/html;charset=UTF-8" language="java"
  %>
 <html>
 <%
 /*
- * Copyright 2002,2004 The Apache Software Foundation.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
+* Copyright 2002,2004 The Apache Software Foundation.
+*
+* Licensed under the Apache License, Version 2.0 (the "License");
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+*      http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
 %>
 
 <%!
     public void jspInit(){
         ServletContext context = this.getServletConfig().getServletContext();
         File repoDir = new File(context.getRealPath("/WEB-INF"));
-        File serviceDir = new File(repoDir,DeploymentConstants.SERVICE_PATH);
+        File serviceDir = new File(repoDir,"services");
 
         if (!serviceDir.exists()) {
             serviceDir.mkdir();
@@ -53,79 +57,61 @@
     }
 %>
 <head>
- <title> Upload a service </title>
- <link href="css/axis-style.css" rel="stylesheet" type="text/css">
+<title> Upload a service </title>
+<link href="css/axis-style.css" rel="stylesheet" type="text/css">
 </head>
-	<body>
+<body>
 	    <jsp:include page="include/header.inc"></jsp:include>
+         <h2>Upload a service jar file</h2>
         <%
-            String contentType = request.getContentType();
-            if (contentType!=null && contentType.indexOf("multipart/form-data")>=0){
-
-                DataInputStream in = new DataInputStream(request.getInputStream());
-                int formDataLength = request.getContentLength();
-                File outputFile = null;
-                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);
-                String saveFile = file.substring(file.indexOf("filename=\"") + 10);
-                saveFile = saveFile.substring(0, saveFile.indexOf("\n"));
-                if (saveFile.indexOf("\\")<0){
-                     saveFile = saveFile.substring(saveFile.lastIndexOf("/") + 1,saveFile.indexOf("\""));
-                }else{
-                     saveFile = saveFile.substring(saveFile.lastIndexOf("\\") + 1,saveFile.indexOf("\""));
-                }
-
-                saveFile = saveFile.toLowerCase();
-
-                if (!saveFile.endsWith(".jar")){
-                    writeUnsuccessMessage("Wrong file type!!",out);
-                    %>
-                    <jsp:include page="include/link-footer.inc"></jsp:include>
-                    <jsp:include page="include/footer.inc"></jsp:include><%
-                    return;
+            boolean isMultipart = FileUpload.isMultipartContent(request);
+            if (isMultipart){
+                try {
+                    // Create a new file upload handler
+                    DiskFileUpload upload = new DiskFileUpload();
+
+                    List items = upload.parseRequest(request);
+
+                    // Process the uploaded items
+                    Iterator iter = items.iterator();
+                    while (iter.hasNext()) {
+                        FileItem item = (FileItem) iter.next();
+
+                        if (!item.isFormField()) {
+
+                            String fileName = item.getName();
+                            fileName = fileName.toLowerCase();
+                            if (!(fileName.endsWith(".jar")||fileName.endsWith(".aar"))){
+                                throw new Exception(" Wrong file type! ");
+                            }
+
+                            String fileNameOnly = "";
+                            if (fileName.indexOf("\\")<0){
+                                  fileNameOnly= fileName.substring(fileName.lastIndexOf("/")+1,fileName.length());
+                            }else{
+                                 fileNameOnly= fileName.substring(fileName.lastIndexOf("\\")+1,fileName.length());
+                            }
+
+
+                            File uploadedFile = new File(deploymentDirectory,fileNameOnly);
+                            item.write(uploadedFile);
+                            out.write("<font color=\"green\">File " + fileName + " successfully uploaded </font><br/><br/>");
+
+                        }
+                    }
+                } catch (Exception e) {
+                    out.write(" <font color=\"red\">File upload failed! <br/>" + e.getMessage() + "</font><br/><br/>");
                 }
+            }
+        %>
 
-                outputFile = new File(deploymentDirectory,saveFile);
-
-                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;
-
-                FileOutputStream fileOutputStream = new FileOutputStream(outputFile);
-                //fileOutputStream.write(dataBytes);
-                fileOutputStream.write(dataBytes, startPos, (endPos - startPos));
-                fileOutputStream.flush();
-                fileOutputStream.close();
-
-                writeSuccessMessage(saveFile,out);
+        You can upload a properly packaged Axis 2 service by selecting the file and clicking upload
+        <form method="post"  name="Axis2upload" action="upload.jsp" enctype="multipart/form-data">
+        <input type="file" name="filename" chars="50"/><br/>
+        <input name="<%=SUBMIT_NAME%>" type="submit" value=" Upload "/>
+        </form>
 
-            }else{
-        %>
-		<h2>Upload a service jar file</h2>
-		You can upload a properly packaged Axis 2 service by selecting the file and clicking upload
-       <form method="post"  name="Axis2upload" action="upload.jsp" enctype="multipart/form-data">
-         <input type="file" name="filename" chars="50"/><br/>
-         <input name="<%=SUBMIT_NAME%>" type="submit" value=" Upload "/>
-       </form>
-       <%}%>
        <jsp:include page="include/link-footer.inc"></jsp:include>
        <jsp:include page="include/footer.inc"></jsp:include>
-	</body>
+       </body>
 </html>