【发布时间】:2012-03-20 12:55:01
【问题描述】:
论坛成员我在 extjs 4 和 JAVA 中上传文件时遇到了一些问题。
我用的是extjs4,上传表单代码是
{
xtype: 'filefield',
margin: '10 0 0 5',
width: 296,
fieldLabel: 'Image',
emptyText: 'Select Company logo...',
id: 'cmplogo',
itemId: 'cmplogo',
name: 'cmplogo',
labelWidth: 70
},
{
xtype: 'button',
margin: '10 0 0 90',
width: 89,
text: 'Upload Image',
action: 'btn-upload-img'
},
按钮上传图片有action:'btn-upload-img',函数定义如下
fileUpload: function(btn) {
var win = btn.up('window');
var form = win.down('form').getForm();
alert('VALUE IS :'+form.getValues());
if(form.isValid()){
form.submit({
url : 'company/UploadFile.action',
waitMsg: 'Uploading your file...',
success: function(fp, o) {
Ext.Msg.alert('Success', 'Your file has been uploaded.');
}
});
}
},
我的java控制器有以下功能代码
@RequestMapping(value = "/company/UploadFile.action")
public @ResponseBody
Map<String, ? extends Object> uploadFile(UploadItem uploadItem, BindingResult result) throws Exception {
System.out.println("QUERY TO UPLOAD FILE");
try {
if(result.hasErrors()) {
for(ObjectError error: result.getAllErrors()) {
System.err.println("Error: "+error.getCode()+" - "+error.getDefaultMessage());
}
}
else
{
MultipartFile file = uploadItem.getFileData();
String fileName = null;
InputStream inputStream = null;
OutputStream outputStream = null;
if(file.getSize() > 0) {
inputStream = file.getInputStream();
if(file.getSize() > 10000) {
System.out.println("FILE SIZE:::"+file.getSize());
}
System.out.println("SIZE ::"+file.getSize());
fileName = "E:\\images\\"+file.getOriginalFilename();
outputStream = new FileOutputStream(fileName);
System.out.println("FILE NAME AND PATH IS ::"+fileName);
System.out.println("FILENNAME ::"+file.getOriginalFilename());
int readBytes = 0;
byte[] buffer = new byte[10000];
while((readBytes = inputStream.read(buffer, 0, 10000)) !=-1) {
outputStream.write(buffer, 0, readBytes);
}
outputStream.close();
inputStream.close();
}
}
} catch (Exception e) {
return getModelMapError("Error trying to read city");
}
return null;
}
不知道我上面的代码有什么问题。 我的萤火虫控制台给了我以下错误
**uncaught exception: You're trying to decode an invalid JSON String: <pre>{"message":"Error trying to read city","success":false}</pre>**
请给我一些解决方案,我可以尝试尽快解决我的错误。
【问题讨论】:
-
您能找到适合您情况的解决方案吗?
标签: java spring extjs4 extjs-mvc