无法将图片上传到网络服务器

时间:2013-05-18 22:47:46

标签: java spring-mvc image-uploading

我创建了两个项目,一个用于客户端,另一个用于服务器端。它似乎不起作用。我认为它可能与POST请求有关,与Web表单的文件格式不匹配。我想将图片发送到网络服务器。首先,我想从我的电脑和未来的Android手机上做到这一点。这是我得到的代码:

客户方:

public class Send {
public static void main(String[] args) throws Exception {
    String filePath = "C:\\Users\\Mat\\Desktop\\Pic.bmp";
    String picName = "Pic.bmp";

    HttpClient httpclient = new DefaultHttpClient();

    try {
        HttpPost httppost = new HttpPost("http://localhost:8080/springmvc/upload");

        FileBody pic = new FileBody(new File(filePath));
        StringBody name = new StringBody(picName);

        MultipartEntity requestEntity = new MultipartEntity();
        requestEntity.addPart("text", name);
        requestEntity.addPart("file", pic);

        httppost.setEntity(requestEntity);

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

        System.out.println("----------------------------------------");
        System.out.println(response.getStatusLine());
        if (responseEntity != null) {
            System.out.println("Response content length: " + responseEntity.getContentLength());
        }
        EntityUtils.consume(responseEntity);
    } finally {
        try {
            httpclient.getConnectionManager().shutdown();
        } 
        catch (Exception ignore) {
        }
    }
}
}

来自“System.out.println(response.getStatusLine());”我得到输出“HTTP / 1.1 400 Bad Request”


服务器端(Web服务器):

控制器:

@Controller
public class HomeController {@RequestMapping(value = "/upload", method = RequestMethod.POST)
public String handleFormUpload(@RequestParam("name") String name, @RequestParam("file") MultipartFile file) throws IOException {
    if (!file.isEmpty()) {
        byte[] bytes = file.getBytes();
        FileOutputStream fos = new FileOutputStream(
                "C:\\Users\\Mat\\Desktop\\image.bmp");
        try {
            fos.write(bytes);
        } finally {
            fos.close();
        }
        return "works";
    } else {
        return "doesn't work";
    }
}

}

.jsp文件(我猜的形式):

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page session="false" %>
<html>
<head>
    <title>Upload a file please</title>
</head>
<body>
    <h1>Please upload a file</h1>
    <form method="post" action="/form" enctype="multipart/form-data">
        <input type="text" name="name"/>
        <input type="file" name="file"/>
        <input type="submit"/>  
    </form>
</body>

豆类:

    <bean id="multipartResolver"
    class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
</bean> 

<!-- Declare explicitly, or use <context:annotation-config/> -->
<bean id="HomeController" class="net.codejava.springmvc.HomeController">

</bean>

添加了dependecies(我从MVC模板创建了一个项目,所以从一开始就有一堆):

<!--  UploadImage -->
    <dependency>
        <groupId>commons-fileupload</groupId>
        <artifactId>commons-fileupload</artifactId>
        <version>1.3</version>
    </dependency>

我希望客户端发送具有特定名称的图片,而无需从客户端进行任何手动工作。

我正在使用STS和Spring MVC框架。

如果有人能看到我做错了什么,我将不胜感激! 先谢谢!

1 个答案:

答案 0 :(得分:0)

问题是形式预期a:

<input type="text" name="name"/> 

当我删除该行时,它有效!

相关问题