我想上傳與其它字段文件,但瀏覽器顯示的錯誤:「HTTP狀態400 - 客戶端發送的請求是語法不正確的()」ExtJS的和Spring MVC文件上傳失敗
這裏是我的Java代碼:
@RequestMapping(value = "/save.do", method = RequestMethod.POST)
public @ResponseBody
void saveUser(
@RequestBody SystemUser systemUser,
@RequestParam("file") MultipartFile file,
BindingResult result) {
if (result.hasErrors()) {
for (ObjectError error : result.getAllErrors()) {
System.err.println("Error: " + error.getCode() + " - "
+ error.getDefaultMessage());
}
}
String fileName = file.getOriginalFilename();
try {
byte[] bytes = file.getBytes();
// Creating the directory to store file
String rootPath = System.getProperty("catalina.home");
File dir = new File(rootPath + File.separator + "img");
if (!dir.exists())
dir.mkdirs();
// Create the file on server
File serverFile = new File(dir.getAbsolutePath() + File.separator
+ fileName);
BufferedOutputStream stream = new BufferedOutputStream(
new FileOutputStream(serverFile));
stream.write(bytes);
stream.close();
systemUser.setImageFile(fileName);
systemUserService.create(systemUser);
} catch (Exception e) {
e.printStackTrace();
}
}
這裏是我的JS代碼:
userformAdd = Ext.create('Ext.window.Window',{
alias : 'widget.usersform',
title : 'Add User',
id : 'add',
width : 350,
layout : 'fit',
resizable: false,
closeAction: 'hide',
modal : true,
config : {
recordIndex : 0,
action : ''
},
items : [{
xtype : 'form',
layout: 'anchor',
bodyStyle: {
background: 'none',
padding: '10px',
border: '0'
},
defaults: {
xtype : 'textfield',
anchor: '100%'
},
items : [{
name : 'id',
fieldLabel: 'ID'
},{
name: 'fullName',
fieldLabel: 'Full Name'
},{
name: 'address1',
fieldLabel: 'Address 1'
},{
name: 'address2',
fieldLabel: 'Address 2'
},{
name: 'contact1',
fieldLabel: 'Contact 1'
},{
name: 'contact2',
fieldLabel: 'Contact 2'
},{
xtype: 'fileuploadfield',
name: 'file',
fieldLabel: 'File',
labelWidth: 50,
msgTarget: 'side',
allowBlank: false,
anchor: '100%',
buttonText: 'Select a File...'
}]
}],
buttons: [{
text: 'OK',
handler : function (btn){
var values = btn.up('window').down('form').getForm();
values.submit({
url: 'module/save.do',
success: function(fp, o) {
var grid = Ext.ComponentQuery.query('grid')[0];
grid.getStore().load();
}
});
userformAdd.close();
;
}
},{
text : 'Reset',
handler : function() {
this.up('window').down('form').getForm().reset();
}
},{
text : 'Cancel',
handler: function() {
this.up('window').close();
}
}]
});
而且我還用於multipartResolver豆在xml配置:
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- one of the properties available; the maximum file size in bytes -->
<property name="maxUploadSize" value="5000000" />
</bean>
不知道我在做什麼錯在這裏...任何建議/幫助將不勝感激。 感謝
HTTP本身存在問題(錯誤400)......它沒有解決問題:( –
請再次嘗試使用方法聲明我提供。 @ResponseBody指出,'返回一個值',但你有一個'void'定義。 –
我宣佈了註釋,因爲您提供並刪除了@ResponseBody,但問題仍然是一樣的... –