Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jsp 如何在服务器上上传文件?_Jsp - Fatal编程技术网

Jsp 如何在服务器上上传文件?

Jsp 如何在服务器上上传文件?,jsp,Jsp,我想上传一个pdf从客户端到服务器。 我必须使用以下代码 <%@ page import="java.io.*" %> <% String contentType = request.getContentType(); if ((contentType != null) && (contentType.indexOf("multipart/form-data") >= 0)) { DataInputStream in =

我想上传一个pdf从客户端到服务器。 我必须使用以下代码

    <%@ 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;
    saveFile="http://hpws1/shared_mxd/pdf/" + saveFile;

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

    %><Br><table border="2"><tr><td><b>You have successfully

= 0))     {
DataInputStream in=新的DataInputStream(request.getInputStream());
int formDataLength=request.getContentLength();
字节数据字节[]=新字节[formDataLength];
int byteRead=0;
int totalBytesRead=0;
while(totalBytesRead
您已经成功
以以下名称上载文件:

其中“saveFile=”http://hpws1/shared_mxd/pdf/“+saveFile;”是服务器的路径。 但浏览器无法识别此路径,并出现错误“文件名、目录名或卷标语法不正确”。
如何解决dis???

为什么不使用表单发送文件?如下所示:

<FORM ENCTYPE='multipart/form-data'
 method='POST' action='/myservlet'>
<INPUT TYPE='file' NAME='mptest'>
<INPUT TYPE='submit' VALUE='upload'>
</FORM>


当然,我必须使用如图所示的表单。它工作正常。问题是:如果我在本地系统本身保存文件,上面的代码对我有效,但如果我想将其保存在服务器位置“”,它不工作。您想将文件保存在哪里?