[英]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.
我认为这可能与POST请求与Web表单的文件格式不匹配有关。 I would like to send a picture to a web server.
我想将图片发送到Web服务器。 To begin with I'd like to do it from my computer and in the future from an android phone.
首先,我想从我的计算机上进行此操作,将来希望通过Android手机进行。 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());" 来自“ System.out.println(response.getStatusLine());” I get the output "HTTP/1.1 400 Bad Request"
我得到输出“ HTTP / 1.1 400 Bad Request”
Server side (web server): 服务器端(Web服务器):
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): .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>
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): 增加了依赖性(我从MVC模板创建了一个项目,因此从一开始就有很多):
<!-- 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. 我正在使用STS和Spring MVC框架。
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! 当我删除该行时,它起作用了!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.