我想在上传后重定向,并希望重定向"文件"页。但提交成功后,浏览器中的网址没有重定向,我也使用firebug检查是否有重定向,但没有发生。 我更改了"重定向:/ files" to" redirect:/files.jsp"也没有帮助。 有什么错吗?
这是我的代码:
@Controller
@RequestMapping({ "/files", "/files/" })
public class FileAdminController {
@RequestMapping(value = { "/upload/index", "/upload", "/upload/" }, method = RequestMethod.GET)
public String showUplaod() {
return "upload";
}
@RequestMapping(value = { "/index", "/index/", "/" }, method = RequestMethod.GET)
public String showFilePage() {
return "files";
}
@RequestMapping(value = "/upload", method = RequestMethod.POST)
public @ResponseBody String handleFileUpload(
@RequestParam("jarName") String jarName,
@RequestParam("manifestName") String manifestName,
@RequestParam("files") MultipartFile file) {
try {
File file1 = new File("c:/uploads/");
file1.getParentFile().mkdirs();
file1.createNewFile();
BufferedOutputStream stream = new
BufferedOutputStream(
new FileOutputStream(file1));
stream.write(bytes);
stream.close();
return "redirect:/files";
} catch (Exception e) {
return "You failed to upload " + jarName + " => " + e.getMessage();
}
}
}
答案 0 :(得分:6)
redirect:
是一个视图名称,由UrlBasedViewResolver
解析。但是对于@ResponseBody
,您告诉spring该控制器没有返回视图名称。即您必须通过注入HttpServletResponse
来自行注意重定向。