Java 使用jsp作为服务器端脚本将文件上载到特定文件夹

Java 使用jsp作为服务器端脚本将文件上载到特定文件夹,java,jsp,servlets,uploadify,Java,Jsp,Servlets,Uploadify,正如标题所暗示的,我正试图将一个文件上传到本地服务器,为此,我使用了一个JSP和uploadify(基于flash的jquery上传程序)。 我已经使用glassfish server 3.1成功上传了一个文件,这是我的HTML代码: </head> <body> <table> <tr> <td> <input id="file_upload" na

正如标题所暗示的,我正试图将一个文件上传到本地服务器,为此,我使用了一个JSP和uploadify(基于flash的jquery上传程序)。 我已经使用glassfish server 3.1成功上传了一个文件,这是我的HTML代码:

</head>
<body>
    <table>
        <tr>
            <td>
                <input id="file_upload" name="file_upload" type="file" />
                <script type="text/javascript">
                    $('#file_upload').uploadify({
                        'uploader'    : 'uploadify/uploadify.swf',
                        'script'      : 'UploadFile/uploadFile.jsp',
                        'cancelImg'   : 'uploadify/cancel.png',
                        'multi'       : true,
                        'auto'        : false,
                        'onComplete'  : function(fileObj) 
                        {
                            alert (fileObj.filePath); //The path on the server to the uploaded file
                        }
                    });
                </script>

            </td>
            <td>
                <a href="javascript:$('#file_upload').uploadifyUpload($('.uploadifyQueueItem').last().attr('id').replace('file_upload',''));">Upload Last File</a>
            </td>
        </tr>
    </table>
</body>

$('#文件上传')。上传({
'uploader':'uploadify/uploadify.swf',
'script':'UploadFile/UploadFile.jsp',
'cancelImg':'uploadify/cancel.png',
"多":对,,
“自动”:false,
“onComplete”:函数(fileObj)
{
警报(fileObj.filePath);//服务器上上载文件的路径
}
});
这是我的服务器端脚本:

<%@ page import="java.io.*" %>
<%
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);

    String 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;

    FileOutputStream fileOut = new FileOutputStream(saveFile);
    fileOut.write(dataBytes, startPos, (endPos - startPos));
    fileOut.flush();
    fileOut.close();
%>

<table>
    <tr>
        <td>
            <b>File uploaded:</b>
            <% out.println(saveFile);%>
        </td>
    </tr>
</table> 

 <%} else {
    out.println(contentType.indexOf("multipart/form-data")); 
    out.println(request.getContentType()); 
%>
        <br/> error <% } %>

= 0)) {
DataInputStream in=新的DataInputStream(request.getInputStream());
int formDataLength=request.getContentLength();
字节数据字节[]=新字节[formDataLength];
int byteRead=0;
int totalBytesRead=0;
while(totalBytesRead
上载的文件:

错误
所以我的问题是,是否可以更改要将内容上载到的默认文件夹? e、 g:我现在的默认文件夹是C:\Users\USERNAME。netbeans\7.0\config\GF3\domain1是否可以将其更改为C:\Users\USERNAME\Desktop

如果我对这个问题还不够清楚,请尽管说,非常感谢您的帮助,谢谢

我现在的默认文件夹是C:\Users\USERNAME。netbeans\7.0\config\GF3\domain1是否可以将其更改为C:\Users\USERNAME\Desktop

您的默认目录恰好是domain1目录,因为您没有在以下行中指定文件的绝对路径:

FileOutputStream fileOut = new FileOutputStream(saveFile);
如果没有绝对路径,文件将保存在相对于Java进程当前工作目录的位置,在Glassfish应用程序服务器的情况下,该目录恰好是域目录。指定绝对路径后,您将能够将上载的文件保存到您选择的位置


在不同的注释中,考虑以下几点:

  • 使用Servlet处理文件上传请求。JSP通常用于为视图生成演示内容
  • 使用Servlet 3.0 API提供的注释来处理文件上传请求;Tomcat 7和Glassfish 3.1依靠编写良好的Apache Commons Fileupload库来处理多部分POST请求。这样,您就不必担心自己处理请求。您可以取而代之的是检索个人把你自己分开,把脏活留在容器里

谢谢你的回答!因此,你的意思是,我应该将路径定义为FileOutputStream fileOut=newfileoutputstream(“C:\\Users\\USERNAME\\Desktop\\uploads”);这是我第一次使用JSP和servlet,非常令人困惑。不,路径应该与
连接C:\\Users\\USERNAME\\Desktop\\uploads“
以便获得绝对文件路径。需要提供给
FileOutputStream
构造函数的不是目录路径,而是绝对文件路径。由于您是新的,所以只需添加,请使用'request.getSession().getServletContext().getRealPath(“/”)'来检索路径。@zawhtut:不,不要这样做。您不想将上载的文件保存在扩展的WAR文件夹中。@BalusC Yea,这是您指出的事实。上载文件夹路径最好在属性文件或数据库中。此代码可以识别为roseindia.net示例之一。请阅读此代码。我强调您要删除这是一段糟糕的代码,可以使用Commons FileUpload或Servlet 3.0
getParts()
。另请参见和