Javascript post:400从angularjs发送post请求时请求错误
我正在写一个上传文件的程序,前端是angularjs,后端是SpringMVC,当我选择文件并单击submit时,我将得到Post400(错误请求) 服务器端的控制器代码是Javascript post:400从angularjs发送post请求时请求错误,javascript,angularjs,spring,spring-mvc,Javascript,Angularjs,Spring,Spring Mvc,我正在写一个上传文件的程序,前端是angularjs,后端是SpringMVC,当我选择文件并单击submit时,我将得到Post400(错误请求) 服务器端的控制器代码是 @RestController public class FileController { @RequestMapping(value="uploads", method=RequestMethod.POST) public ModelAndView handleFileUpload(@ModelAttribute D
@RestController
public class FileController {
@RequestMapping(value="uploads", method=RequestMethod.POST)
public ModelAndView handleFileUpload(@ModelAttribute Document document,@RequestParam("file") MultipartFile file,
HttpServletRequest request,HttpServletResponse response) throws IOException{
System.out.println("in the file Controller");
//ServletContext context = request.getServletContext();
byte[] fileBytes = null;
FileInputStream inputStream = null;
ServletOutputStream outStream = null;
BufferedOutputStream stream = null;
ModelAndView model = new ModelAndView("helloword");
String myfile = file.getOriginalFilename();
String message = "the file is with server "+myfile;
fileBytes = file.getBytes();
model.addObject("message",message);
String rootpath = System.getProperty("catalina.home");
File dir = new File(rootpath +File.separator + "tmpFiles");
if(!dir.exists()){
dir.mkdirs();
}
System.out.println(dir);
File serrverFile = new File(dir.getAbsolutePath() + File.separator + "download");
stream = new BufferedOutputStream(new FileOutputStream(serrverFile));
stream.write(fileBytes);
stream.close();
/* String serverFileName = serrverFile.getAbsolutePath();
System.out.println(serverFileName);
File fileDownload = new File(serverFileName);
try {
inputStream = new FileInputStream(fileDownload);
} catch (FileNotFoundException e) {
e.printStackTrace();
}*/
/*String mimeType = context.getMimeType(serverFileName);
if(mimeType == null){
mimeType = "application/octet-stream";
}
response.setContentType(mimeType);
response.setContentLength(file.getSize());
String headerKey = "Content-Deposition";
String headerValue = String.format("attachment; filename=\"%s\"",file.getName());
response.setHeader(headerKey, headerValue);
try {
outStream = response.getOutputStream();
} catch (IOException e) {
e.printStackTrace();
}
byte[] buffer = new byte[1000000000];
int bytesRead = -1;
try {
while((bytesRead = inputStream.read(buffer))!= -1){
outStream.write(buffer, 0 , bytesRead);
}
} catch (IOException e) {
e.printStackTrace();
}
try {
inputStream.close();
outStream.close();
} catch (IOException e) {
e.printStackTrace();
}
*/
return model;
}
如果我使用jsp页面而不是angularjs,那么它工作得很好,但我需要anjularjs中的它。所以我在这里遗漏了什么。请让我知道
@RestController
public class FileController {
@RequestMapping(value="uploads", method=RequestMethod.POST)
public ModelAndView handleFileUpload(@ModelAttribute Document document,@RequestParam("file") MultipartFile file,
HttpServletRequest request,HttpServletResponse response) throws IOException{
System.out.println("in the file Controller");
//ServletContext context = request.getServletContext();
byte[] fileBytes = null;
FileInputStream inputStream = null;
ServletOutputStream outStream = null;
BufferedOutputStream stream = null;
ModelAndView model = new ModelAndView("helloword");
String myfile = file.getOriginalFilename();
String message = "the file is with server "+myfile;
fileBytes = file.getBytes();
model.addObject("message",message);
String rootpath = System.getProperty("catalina.home");
File dir = new File(rootpath +File.separator + "tmpFiles");
if(!dir.exists()){
dir.mkdirs();
}
System.out.println(dir);
File serrverFile = new File(dir.getAbsolutePath() + File.separator + "download");
stream = new BufferedOutputStream(new FileOutputStream(serrverFile));
stream.write(fileBytes);
stream.close();
/* String serverFileName = serrverFile.getAbsolutePath();
System.out.println(serverFileName);
File fileDownload = new File(serverFileName);
try {
inputStream = new FileInputStream(fileDownload);
} catch (FileNotFoundException e) {
e.printStackTrace();
}*/
/*String mimeType = context.getMimeType(serverFileName);
if(mimeType == null){
mimeType = "application/octet-stream";
}
response.setContentType(mimeType);
response.setContentLength(file.getSize());
String headerKey = "Content-Deposition";
String headerValue = String.format("attachment; filename=\"%s\"",file.getName());
response.setHeader(headerKey, headerValue);
try {
outStream = response.getOutputStream();
} catch (IOException e) {
e.printStackTrace();
}
byte[] buffer = new byte[1000000000];
int bytesRead = -1;
try {
while((bytesRead = inputStream.read(buffer))!= -1){
outStream.write(buffer, 0 , bytesRead);
}
} catch (IOException e) {
e.printStackTrace();
}
try {
inputStream.close();
outStream.close();
} catch (IOException e) {
e.printStackTrace();
}
*/
return model;
}