struts1文件上传出现问题

发布于 2021-11-22 13:57:11 字数 4840 浏览 700 评论 1

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

乞讨 2021-11-25 09:35:37

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文