诶诶诶略 2016-05-11 07:57 采纳率: 0%
浏览 2027

springmvc+easyui实现图片上传

@RequestMapping(value="/upload",method=RequestMethod.POST)
@ResponseBody
public Json upload(@RequestParam(value="imageFile") MultipartFile file,HttpServletRequest req,HttpServletResponse resp) throws IOException{
String temppath="/image";
Json j = new Json();
//构建文件名称
String oldName = file.getOriginalFilename();
//获取文件的后缀
String suffix = oldName.substring(oldName.indexOf('.'));
String name = String.valueOf(System.currentTimeMillis()).concat(suffix);
String path = req.getSession().getServletContext().getRealPath(temppath);
try {
File filepath = new File(path);
if(!filepath.exists()){
filepath.mkdir(); //创建目录
}
OutputStream os = new FileOutputStream(filepath.getPath() +"/"+name);
InputStream is = file.getInputStream();
byte[] buffer = new byte[1024];
int len = 0;
while((len = is.read(buffer)) != -1){
os.write(buffer, 0, len);
}
os.flush();
os.close();
is.close();
j.setSuccess(true);
j.setMsg("上传成功");
} catch (Exception e) {
j.setMsg("上传失败");
j.setSuccess(false);
e.printStackTrace();
}
j.setErrorinfo(req.getContextPath()+temppath+"/"+name);
j.setObject(path +"\"+ name);
return j;
}

<tr>
                <td>法人身份证照片</td>
                <td><input type="file" name="imageFile" id="imageFile" onchange="previewImage(this)" accept="image/*"></td>
                <td>
                    <input type="button" onclick="return ajaxFileUpload();" value="上传"/>
                    <input type="hidden" name="legal_url" id="legal_url"/>
                </td>
            </tr>
            <tr>
                <td>营业执照照片</td>
                <td><input type="file" name="imageFile" id="imageFile"></td>
                <td>
                    <input type="button" onclick="return ajaxFileUpload1();" value="上传"/>
                    <input type="hidden" name="busi_url" id="busi_url"/>
                </td>
            </tr>
            <tr>
                <td>公司前台图片</td>
                <td><input type="file" name="imageFile" id="imageFile"></td>
                <td>
                    <input type="button" onclick="return ajaxFileUpload2();" value="上传"/>
                    <input type="hidden" name="comp_url" id="comp_url"/>
                </td>
            </tr>



            //法人身份证照片
function ajaxFileUpload() {
    $.ajaxFileUpload({
        url : '${pageContext.request.contextPath}/commonController/upload',
        type : 'post',
        secureuri:false,
        fileElementId : 'imageFile',
        dataType : 'JSON',
        success : function(data, status) {
            //去掉<pre></pre>标签
             var start = data.indexOf(">");
             if(start != -1) {
               var end = data.indexOf("<", start + 1);
               if(end != -1) {
                 data = data.substring(start + 1, end);
                }
             }
             //转化为JSON对象
            data=eval('('+data+')');
            if (data.success) {
                $("#legal_url").val(data.errorinfo);
                alert("上传成功!");
            }
        },
    });
    return false;
}

//营业执照照片
function ajaxFileUpload1() {
    $.ajaxFileUpload({
        url : '${pageContext.request.contextPath}/commonController/upload',
        type : 'post',
        secureuri:false,
        fileElementId : 'imageFile',
        dataType : 'JSON',
        success : function(data, status) {
            //去掉<pre></pre>标签
             var start = data.indexOf(">");
             if(start != -1) {
               var end = data.indexOf("<", start + 1);
               if(end != -1) {
                 data = data.substring(start + 1, end);
                }
             }
             //转化为JSON对象
            data=eval('('+data+')');
            if (data.success) {
                $("#busi_url").val(data.errorinfo);
                alert("上传成功!");
            }
        },
    });
    return false;
}

//公司前台照片
function ajaxFileUpload2() {
    $.ajaxFileUpload({
        url : '${pageContext.request.contextPath}/commonController/upload',
        type : 'post',
        secureuri:false,
        fileElementId : 'imageFile',
        dataType : 'JSON',
        success : function(data, status) {
            //去掉<pre></pre>标签
             var start = data.indexOf(">");
             if(start != -1) {
               var end = data.indexOf("<", start + 1);
               if(end != -1) {
                 data = data.substring(start + 1, end);
                }
             }
             //转化为JSON对象
            data=eval('('+data+')');
            if (data.success) {
                $("#comp_url").val(data.errorinfo);
                alert("上传成功!");
            }
        },
    });
    return false;
}
  • 写回答

2条回答

  • 诶诶诶略 2016-05-11 07:59
    关注

    上传第一个没问题,第三个上传的图片跟第一个一样,第二个无图片

    评论

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?