Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/276.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
C#文件上传到Java EE服务器_Java_C#_Jakarta Ee - Fatal编程技术网

C#文件上传到Java EE服务器

C#文件上传到Java EE服务器,java,c#,jakarta-ee,Java,C#,Jakarta Ee,我的问题是,上传后的文件内容是[object object] 如何正确上传文件 服务器: package com.turbulence6th.servlets; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import javax.servlet.ServletException; import javax.servl

我的问题是,上传后的文件内容是
[object object]

如何正确上传文件

服务器:

package com.turbulence6th.servlets;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;

@WebServlet("/saveFile")
@MultipartConfig
public class SaveFile extends HttpServlet {
private static final long serialVersionUID = 1L;

protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    String webAppPath = getServletContext().getRealPath("/");
    Part file = request.getPart("file");

    String filename = getFileName(file);
    InputStream is = file.getInputStream();

    String directoryPath = webAppPath + File.separator + "files";
    File directory = new File(directoryPath);

    if(!directory.exists()){
        directory.mkdir();
    }

    String filePath = directoryPath + File.separator + filename;

    FileOutputStream fos = new FileOutputStream(filePath);
    int read = 0;
    byte[] bytes = new byte[1024];

    while ((read = is.read(bytes)) != -1) {
        fos.write(bytes, 0, read);
    }
    fos.close();

}

private String getFileName(Part part) {
    for (String content : part.getHeader("content-disposition").split(";")) {
        if (content.trim().startsWith("filename")) {
            return content.substring(
                    content.indexOf('=') + 1).trim().replace("\"", "");
        }
    }
    return null;
}

}
客户:

 using (var wb = new WebClient())
 {
   wb.UploadFile("http://" + host + ":8080/saveFile", "POST", path);
 }

我看不到对
响应的任何操作。在
doPost
内部,您可以将文件内容复制到
请求中。这是你想要的吗

顺便说一下,我看不出有什么理由不使用