我正在使用按部分划分的multipart / form-data POST请求。
POST .... HTTP/1.1
.
.
.
---boundary123
Content-type:application/octet-stream
content-Disposition: form-data filenale="payload.txt" name="someuniquename"
[paylaod content](this is in xml format)
---boundary123
content-type:application/json
content-Disposition:form-data name="someuniquname"
{ID:"999"}
---boundary123
如何处理此多部分请求?我还希望在使用Spring4和REST发出POST请求之前验证数据。
答案 0 :(得分:1)
你应该遵循以下代码:
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
@Controller
public class FileUploadController {
@RequestMapping(value="/singleUpload")
public String singleUpload(){
return "singleUpload";
}
@RequestMapping(value="/singleSave", method=RequestMethod.POST )
public @ResponseBody String singleSave(@RequestParam("file") MultipartFile file, @RequestParam("desc") String desc ){
System.out.println("File Description:"+desc);
String fileName = null;
if (!file.isEmpty()) {
try {
fileName = file.getOriginalFilename();
byte[] bytes = file.getBytes();
BufferedOutputStream buffStream =
new BufferedOutputStream(new FileOutputStream(new File("F:/cp/" + fileName)));
buffStream.write(bytes);
buffStream.close();
return "You have successfully uploaded " + fileName;
} catch (Exception e) {
return "You failed to upload " + fileName + ": " + e.getMessage();
}
} else {
return "Unable to upload. File is empty.";
}
}
@RequestMapping(value="/multipleUpload")
public String multiUpload(){
return "multipleUpload";
}
@RequestMapping(value="/multipleSave", method=RequestMethod.POST )
public @ResponseBody String multipleSave(@RequestParam("file") MultipartFile[] files){
String fileName = null;
String msg = "";
if (files != null && files.length >0) {
for(int i =0 ;i< files.length; i++){
try {
fileName = files[i].getOriginalFilename();
byte[] bytes = files[i].getBytes();
BufferedOutputStream buffStream =
new BufferedOutputStream(new FileOutputStream(new File("F:/cp/" + fileName)));
buffStream.write(bytes);
buffStream.close();
msg += "You have successfully uploaded " + fileName +"<br/>";
} catch (Exception e) {
return "You failed to upload " + fileName + ": " + e.getMessage() +"<br/>";
}
}
return msg;
} else {
return "Unable to upload. File is empty.";
}
}
}
答案 1 :(得分:0)
@RequestMapping(value = "/upload",
method = RequestMethod.POST,
consumes = MediaType.MULTIPART_FORM_DATA_VALUE,
produces = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody String upload(
@RequestParam("file") final MultipartFile file,
@RequestParam("name") final String name) {
// your work here using the file. you can use file.getBytes()
// or get an input stream from file using file.getInputStream()
// and save it.
return "{\"status\" : \"success\"}";
}
您将在file参数中获取文件,并在名称参数中获取String参数名称。
答案 2 :(得分:0)
这是我使用Spring 4处理Multipart请求的方式(当你不知道Multipart Request中的文件名或文件总数时就是这种情况)
import org.springframework.web.multipart.MultipartHttpServletRequest;
@RequestMapping(value = "/your_webservice_path", method = RequestMethod.POST, consumes = "multipart/form-data")
public Void myController( MultipartHttpServletRequest request) throws Exception
{
Iterator<String> iterator = request.getFileNames();
while (iterator.hasNext()) {
String str=iterator.next().toString();
try{
//extract the file from request
}
catch(Exception ex){
logger.error("Error while parsing the Payload ro metadata from request", ex);
}
}
}
在Jersey2:
import org.glassfish.jersey.media.multipart.MultiPart;
@POST
@Consumes({MediaType.MULTIPART_FORM_DATA})
@Path("/your_webservice_path")
public void myController(MultiPart request) throws Exception {
//use request .getBodyParts() for extracting files from Multipart request
}