开发者社区> 问答> 正文

struts1文件上传出现问题?报错

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

展开
收起
爱吃鱼的程序员 2020-06-22 19:20:53 546 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    现在我也是困在这个问题上了

    2020-06-22 19:21:11
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关课程

更多

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载