ajax上传文件到linux服务器

前端之家收集整理的这篇文章主要介绍了ajax上传文件到linux服务器前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

页面引包

<script src="${ctx}/static/jquery-validation/jquery.validate.min.js" type="text/javascript"></script>
<script src="${ctx}/static/jquery-validation/jquery.Metadata.js" type="text/javascript"></script>
<script src="${ctx}/static/jquery-validation/messages_cn.js" type="text/javascript"></script>
<script src="${ctx}/static/jquery-form/jquery.form.js" type="text/javascript"></script>
<link href="${ctx}/static/javascripts/ajax-upload/fileuploader.css" rel="stylesheet" type="text/css" />
<script src="${ctx}/static/javascripts/ajax-upload/fileuploader.js"></script>


<tr>
            <td align="right" valign="top" class="l-table-edit-td">产品描述第一张图片:</td>
            <td align="left" class="l-table-edit-td">
                <div id="picture_original_size" ${string0 == null? "style=display:none": ""}>
                    <img id="reCommImgsizeChart" src="${pageContext.request.remoteAddr }${string0}"/>
                </div>
                <input type="hidden" name="describeOnePicUrl" id="describeOnePicUrl" value="${string0}"/>
                  <br/>
                <div id="file-uploader_size">
                    <script type="text/javascript">
                        function createUploaderSize() {
                            new qq.FileUploader({
                                element: document.getElementById('file-uploader_size'),action: '${ctx}/account/uplPic',debug: false,allowedExtensions: ['jpg','jpeg','png','gif'],template: '<div class="qq-uploader">' +
                                        '<div class="qq-upload-button">上传图片</div>' +
                                        '<ul class="qq-upload-list"></ul>' +
                                        '</div>',fileTemplate: '<li>' +
                                        '<span class="qq-upload-file"></span>' +
                                        '<span class="qq-upload-spinner"></span>' +
                                        '<span class="qq-upload-size"></span>' +
                                        '<a class="qq-upload-cancel" href="#">取消上传</a>' +
                                        '<span class="qq-upload-Failed-text">上传失败</span>' +
                                        '</li>',onComplete: function(id,fileName,responseJSON) {
                                    $("#picture_original_size").show();
                                    if (responseJSON.success) {
                                        $("#reCommImgsizeChart").attr("src",responseJSON.picurl);
                                        $("#describeOnePicUrl").val(responseJSON.picurl);
                                    }
                                }
                            });
                        }
                        createUploaderSize();

                    </script>
                </div>
            </td><td align="left"></td>
        </tr>

controller:
//上传图片
	@RequestMapping(value = "uplPic")
    @ResponseBody
    public String uploadPic(HttpServletRequest request,HttpServletResponse response) throws Exception {
		logger.info("========================调用uploadPic"+"方法===============================");
		ServletInputStream inputs = request.getInputStream();
		logger.info("========================值inputs"+"===============================");
        return  mallProductRuleService.getUploadPic(inputs,request);
    }

service:
public  static  final  String webUrl =  JobConfig.getValue("replace.web.url");
    public  static  final   String storageFolder = JobConfig.getValue("crawler.cookiestar.storageFolder");

//球衣产品图片上传
	public String getUploadPic(ServletInputStream inputs,HttpServletRequest request) throws IOException {
		String fileUrl = "";
        byte[] fileData = IoUtils.toByteArray(inputs);
        logger.info("=============="+fileData+"为==============================================");
        String hashedName = DigestUtils.md5Hex(fileData);
        String savePath = File.separator + hashedName.substring(0,2) + File.separator + hashedName.substring(2,4) + File.separator + hashedName;
        try{    
        //服务器地址
        File saveFile = new File(storageFolder+savePath+".png");
        String url = webUrl + savePath.replace("\\","/")+".png";
        logger.info("===================================================================图片上传路径:"+url);
        //传递不含ip的url
        int size = url.indexOf("/cmsresource");
        logger.info("===================================================================size="+size);
	    fileUrl = url.substring(size,url.length());
		logger.info("===================================================================图片保存数据库路径:"+fileUrl);
		FileUtils.writeByteArrayToFile(saveFile,fileData);
		logger.info("===================================================================success");
        }catch(Exception e){
        	logger.info("===================================================================exception:",e);
        	return "{fail: false,picurl:\"" + fileUrl + "\"}";
        }
        return "{success: true,picurl:\"" + fileUrl + "\"}";
		
    }


配置文件

crawler.cookiestar.storageFolder=/cmsresource/images/shop



replace.web.url = 192.168.0.235/cmsresource/images/shop
原文链接:https://www.f2er.com/ajax/165959.html

猜你在找的Ajax相关文章