html
<input type="file" id = "upload_file" name = "Filedata">
js
$.ajaxFileUpload({
url:'url',
type:'POST',
secureuri:false,
fileElementId:"upload_file",
data:{data:data},
dataType: 'json',
success: function(result){
}
java
@RequestMapping(value = "/upload",method=RequestMethod.POST)
public @ResponseBody
String upload(HttpServletRequest request,@RequestParam("Filedata") CommonsMultipartFile Filedata, @RequestParam("data") long data) {
@RequestMapping(value = "/upload",method=RequestMethod.POST)
public @ResponseBody
String upload(HttpServletRequest request,@RequestParam("Filedata") CommonsMultipartFile Filedata, @RequestParam("groupId") long group_id) {
log.info("test");
String test=request.getUserPrincipal().getName();
int code=ResultCode.SUC_CODE;
String msg="success";
String path = request.getSession().getServletContext()
.getRealPath("/test/");
File file =new File(path);
//如果文件夹不存在则创建
if (!file .exists() && !file .isDirectory())
{
file .mkdir();
}
long now_time =new Date().getTime();
String name=Filedata.getOriginalFilename();
String filePath=path+"\\"+Long.toString(now_time)+"_"+name;
if (!Filedata.isEmpty()) {
try {
byte[] bytes = Filedata.getBytes();
BufferedOutputStream stream =
new BufferedOutputStream(new FileOutputStream(new File(filePath)));
stream.write(bytes);
stream.close();
resource
spring-mvc.xml
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="104857600"/>
<property name="maxInMemorySize" value="4096"/>
</bean>