java - Can't upload picture to web server -
i have created 2 projects, 1 client side , 1 server side. seems not working. figured might have post request isn't matching file format of web form. send picture web server. begin i'd computer , in future android phone. 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); } { try { httpclient.getconnectionmanager().shutdown(); } catch (exception ignore) { } } } }
from "system.out.println(response.getstatusline());" 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); } { fos.close(); } return "works"; } else { return "doesn't work"; } } }
.jsp file (the form suppose):
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <%@ page session="false" %> <html> <head> <title>upload file please</title> </head> <body> <h1>please upload 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 project mvc template there bunch beginning):
<!-- uploadimage --> <dependency> <groupid>commons-fileupload</groupid> <artifactid>commons-fileupload</artifactid> <version>1.3</version> </dependency>
i want client send picture specific name without doing manual work client side.
i using sts , spring mvc framework.
i appreciate if see i'm doing wrong!
on beforehand!
the problem form expected a:
<input type="text" name="name"/>
when removed line worked!
Comments
Post a Comment