jsp
<tr>
<th class="specalt" width="10%">图片</th>
<td class="nobg" width="20%" colspan="3">
<div id="mimg" align="center">
<img id="mpic" name="mpic" border="1" width="100px" height="100px" />
</div>
<input type="hidden" name="images" id="images" value="1.jpg"/>
</td>
</tr>
<form action="${ctx}/ajaxAction.do?method=json&common=uploadphoto&classes=userWymServiceImpl" id="mfrm_grp_add_linkphoto" method="post" enctype="multipart/form-data"> <!-- name="mfrm_grp_add_linkphoto"-->
<table cellpadding="0" cellspacing="1" class="tbcss" width="100%" >
<tr>
<th nowrap="nowrap" class="specalt" align="left">
<div id="uploadphoto" style="display: block;float: left ;width: 49%;text-align: left" >
<input type="file" name="mfile2" id="mfile2" size="50" style="vertical-align:super;height: 1.4em;margin: 0px;padding: 0px;" /> <!-- onblur="upload()" -->
<input type="button" value="上传图片" onclick="upload()"/>
</div>
</th>
</tr>
</table>
</form>
function upload(){
var url=$("#mfile2").val();
if(url==null||url==""){
//$.messager.alert('提示','请选择上传文件!','error');
return alert("请选择上传的文件");
}
alert("two_1");
var options = {
url: 'ajaxAction.do?method=json&common=uploadphoto&classes=userWymServiceImpl',
target: '#uploadOutput',
contentType: 'application/x-www-form-urlencoded',
success: fun_callback2,
type: 'post',
dataType: 'json'
};
method=json&common=uploadPhoto&classes=instrumentServiceImpl");
$('#mfrm_grp_add_linkphoto').ajaxSubmit(options);
alert("上传成功!");
}
function fun_callback2(data){
if(data.jsondata.retCode == "0000"){
var stct='${ctx}';
var src=data.jsondata.content
$("#images").val(src);
$("#mpic").attr("src",stct+"\/"+"wym/images"+"\/"+src);
}else{
$.messager.alert('提示',data.jsondata.retDesc,'error');
}
}
Service层
public Object uploadphoto(HttpServletRequest request) throws Exception {
String filename="";
List fileList = null;
DiskFileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
fileList = upload.parseRequest(request);
for(int i=0;i<fileList.size();i++){
FileItem item = (FileItem)fileList.get(i);
if(!item.isFormField()){
filename=item.getName();
filename=filename.substring(filename.lastIndexOf("\\")+1,filename.length());
logger.info("filename="+filename);
//System.out.println("执行到了这一步");
List<String> list=Arrays.asList(Constans.PICFORMAT);
if(list.contains(filename.split("\\.")[1].toUpperCase())){
filename=GetSn.getSn(request, "G", "F")+"."+filename.split("\\.")[1].toUpperCase();
item.write(new File( request.getRealPath("/") +"wym/images/"+filename));
ReturnValue<String> ret=new ReturnValue<String>();
ret.setRetCode(ReturnCode.SUCCESS);
ret.setRetDesc("上传成功!");
ret.setContent(filename);
return ret;
}else{
ReturnValue<String> ret=new ReturnValue<String>();
ret.setRetCode("0001");
ret.setRetDesc("头像上传失败,请检查图片格式!");
ret.setContent("");
return ret;
}
}else{
continue;
}
}
return upload;
}
报错信息:
18:12:04,982 ERROR AjaxAction:159 - org.apache.commons.fileupload.FileUploadBase$InvalidContentTypeException: the request doesn't contain a multipart/form-data or multipart/mixed stream, content type header is null
18:12:04,983 ERROR AjaxAction:44 - ///////////org.apache.commons.fileupload.FileUploadBase$InvalidContentTypeException: the request doesn't contain a multipart/form-data or multipart/mixed stream, content type header is null
18:12:04,983 INFO AjaxAction:93 - return object class:com.friendone.base.domain.ReturnValue
18:12:04,984 INFO JSONObject:938 - Property 'contentByList' of class com.friendone.base.domain.ReturnValue has no read method. SKIPPED
18:12:04,984 INFO JSONObject:938 - Property 'listToXML' of class com.friendone.base.domain.ReturnValue has no read method. SKIPPED
现在我也是困在这个问题上了
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。