简体   繁体   中英

Can't upload picture to web server

I have created two projects, one for the client side and one for the server side. It seems not to be working. I figured it might have something to do with the POST request isn't matching the file format of the web form. I would like to send a picture to a web server. To begin with I'd like to do it from my computer and in the future from an android phone. This is the code I've got:

Client side:

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) {
        }
    }
}
}

From the "System.out.println(response.getStatusLine());" I get the output "HTTP/1.1 400 Bad Request"


Server side (web server):

Controller:

@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 file (the form I suppose):

<%@ 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>

Beans:

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

Added dependecies (I created a project from a MVC template so there are a bunch from the beginning):

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

I want the client to send a picture with a specific name without doing any manual work from the client side.

I am using STS and the Spring MVC Framework.

I would greatly appreciate if someone could see what I'm doing wrong!
Thanks on beforehand!

The problem was that the form expected a:

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

When I removed that line it worked!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM