Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.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
Java 在独立应用程序中使用spring将文件上载到http_Java_Spring_Apache_Http_File Upload - Fatal编程技术网

Java 在独立应用程序中使用spring将文件上载到http

Java 在独立应用程序中使用spring将文件上载到http,java,spring,apache,http,file-upload,Java,Spring,Apache,Http,File Upload,我正在用java开发一个独立的应用程序。我正在将Spring与Apache服务器一起使用,我想将一个文件上载到服务器。我的问题是,我在客户端收到了很多不推荐的警告。有人能给我一些提示吗 客户端: public void addCourse(ActionEvent e) throws ClientProtocolException, IOException { File file=fileChooser.showOpenDialog(this.mainApp.getCurr

我正在用java开发一个独立的应用程序。我正在将Spring与Apache服务器一起使用,我想将一个文件上载到服务器。我的问题是,我在客户端收到了很多不推荐的警告。有人能给我一些提示吗

客户端:

public void addCourse(ActionEvent e) throws ClientProtocolException, IOException
    {
        File file=fileChooser.showOpenDialog(this.mainApp.getCurrentStage());
        HttpClient httpclient = new DefaultHttpClient();
        httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);

        HttpPost httppost = new HttpPost("http://localhost:8080/ASCourses/filter/upload");


        MultipartEntity mpEntity = new MultipartEntity();
        ContentBody cbFile = new FileBody(file, "multipart/form-data");
        mpEntity.addPart("file", cbFile);


        httppost.setEntity(mpEntity);
        System.out.println("executing request " + httppost.getRequestLine());
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity resEntity = response.getEntity();

        System.out.println(response.getStatusLine());
        if (resEntity != null) {
          System.out.println(EntityUtils.toString(resEntity));
        }
        if (resEntity != null) {
          resEntity.consumeContent();
        }

        httpclient.getConnectionManager().shutdown();
      }
服务器端:

@RequestMapping(value="/upload", method=RequestMethod.POST)
    public @ResponseBody String handleFileUpload( 
            @RequestParam("file") MultipartFile file){
            String name = "test11";
        if (!file.isEmpty()) {
            try {
                byte[] bytes = file.getBytes();
                BufferedOutputStream stream = 
                        new BufferedOutputStream(new FileOutputStream(new File(name + "-uploaded")));
                stream.write(bytes);
                stream.close();
                return "You successfully uploaded " + name + " into " + name + "-uploaded !";
            } catch (Exception e) {
                return "You failed to upload " + name + " => " + e.getMessage();
            }
        } else {
            return "You failed to upload " + name + " because the file was empty.";
        }
    }
另外,我想问你是否有其他上传文件的方法。请记住,我使用的是独立的应用程序,而不是web。 多谢各位