亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Jquery上傳插件Uploadify無刷新上傳文件

發布時間:2020-10-07 00:41:18 來源:網絡 閱讀:761 作者:jethai 欄目:web開發


Uploadify v3.2.1  上傳圖片并預覽


前端JSP:

<script type="text/javascript">
               
            $(function() {
                $("#upload_org_code").uploadify({
                    'height'        : 27, 
                    'width'         : 80,  
                    'buttonText'    : '選擇圖片',
                    'swf'           : '${pageContext.request.contextPath}/js/uploadify/uploadify.swf',
                    'uploader'      : '${pageContext.request.contextPath}/uploadIMGSerlet',
                    'auto'          : true,
                    'multi'         : false,
                    'removeCompleted':false,
                    'cancelImg'     : '${pageContext.request.contextPath}/js/uploadify/uploadify-cancel.png',
                    'fileTypeExts'  : '*.jpg;*.jpge;*.gif;*.png',
                    'fileSizeLimit' : '2MB',
                    'onUploadSuccess':function(file,data,response){
                        $('#' + file.id).find('.data').html('');
                        $("#upload_org_code_name").val(data);
                        $("#upload_org_code_img").attr("src","${pageContext.request.contextPath}/getImg?file="+data);   
                        $("#upload_org_code_img").show();
                    },
                    //加上此句會重寫onSelectError方法【需要重寫的事件】
                    'overrideEvents': ['onSelectError', 'onDialogClose'],
                    //返回一個錯誤,選擇文件的時候觸發
                    'onSelectError':function(file, errorCode, errorMsg){
                        switch(errorCode) {
                            case -110:
                                alert("文件 ["+file.name+"] 大小超出系統限制的" + jQuery('#upload_org_code').uploadify('settings', 'fileSizeLimit') + "大小!");
                                break;
                            case -120:
                                alert("文件 ["+file.name+"] 大小異常!");
                                break;
                            case -130:
                                alert("文件 ["+file.name+"] 類型不正確!");
                                break;
                        }
                    },
                });
         
</script>


后端servlet:

package com.mybank.enterprise.framework.servlet;
 
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;
 
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
 
import com.mybank.enterprise.util.Constant;
import com.mybank.enterprise.util.StringUtil;
 
public class UploadIMGSerlet extends HttpServlet {
 
 
    private static final long serialVersionUID = 1L;
 
    // 上傳文件的保存路徑
    private String configPath = Constant.RB.getString("img_path");
    // 臨時文件路徑
    private String dirTemp = "resource/temp/";
 
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
 
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
         
        String ret_fileName = null;//返回給前端已修改的圖片名稱
 
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html; charset=UTF-8");
        PrintWriter out = response.getWriter();
 
        // 文件保存目錄路徑
        String savePath = configPath;
 
        // 臨時文件目錄
        String tempPath = this.getServletContext().getRealPath("/") + dirTemp;
 
        // 創建文件夾
        File dirFile = new File(savePath);
        if (!dirFile.exists()) {
            dirFile.mkdirs();
        }
 
        // 創建臨時文件夾
        File dirTempFile = new File(tempPath);
        if (!dirTempFile.exists()) {
            dirTempFile.mkdirs();
        }
 
        DiskFileItemFactory factory = new DiskFileItemFactory();
        factory.setSizeThreshold(20 * 1024 * 1024); // 設定使用內存超過5M時,將產生臨時文件并存儲于臨時目錄中。
        factory.setRepository(new File(tempPath));  // 設定存儲臨時文件的目錄。
 
        ServletFileUpload upload = new ServletFileUpload(factory);
        upload.setHeaderEncoding("UTF-8");
 
        try {
            List<?> items = upload.parseRequest(request);
            Iterator<?> itr = items.iterator();
 
            while (itr.hasNext()) {
                FileItem item   = (FileItem) itr.next();
                String fileName = item.getName();
                if(fileName!=null){
                    String endstr = fileName.substring(fileName.indexOf("."),fileName.length());
                    fileName      = StringUtil.createSerial20().concat(endstr);
                    ret_fileName  = fileName;
                }
                if (!item.isFormField()) {
 
                    try {
                        File uploadedFile = new File(savePath,fileName);
 
                        OutputStream os = new FileOutputStream(uploadedFile);
                        InputStream is = item.getInputStream();
                        byte buf[] = new byte[1024];// 可以修改 1024 以提高讀取速度
                        int length = 0;
                        while ((length = is.read(buf)) > 0) {
                            os.write(buf, 0, length);
                        }
                        // 關閉流
                        os.flush();
                        os.close();
                        is.close();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
 
        } catch (FileUploadException e) {
            e.printStackTrace();
        }
        //將已修改的圖片名稱返回前端
        out.print(ret_fileName);
        out.flush();
        out.close();
    }
 
}



顯示圖片servlet

package com.mybank.enterprise.framework.servlet;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
 
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import com.mybank.enterprise.util.Constant;
 
public class GetIMGServlet extends HttpServlet {
     
    private static final long serialVersionUID = 2761789171087122738L;
 
    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
 
        this.doPost(req, resp);
    }
 
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
 
        String file = req.getParameter("file");
         
        File pic = new File(Constant.RB.getString("img_path")+file);
 
        FileInputStream fis = new FileInputStream(pic);
        OutputStream os = resp.getOutputStream();
        try {
            int count = 0;
            byte[] buffer = new byte[1024 * 1024];
            while ((count = fis.read(buffer)) != -1)
                os.write(buffer, 0, count);
            os.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (os != null)
                os.close();
            if (fis != null)
                fis.close();
        }
         
    }
 
}



img標簽的src可以指向servlet只要返回流對象即可,常用的如驗證碼


參考文章:

http://www.cnblogs.com/yangy608/p/3915337.html


向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

盐亭县| 平谷区| 康平县| 九寨沟县| 南华县| 黎川县| 遂川县| 铜陵市| 五大连池市| 伊金霍洛旗| 旬阳县| 德清县| 五指山市| 仙桃市| 开远市| 甘南县| 桂平市| 邯郸市| 福安市| 益阳市| 镇远县| 镇沅| 论坛| 东台市| 岱山县| 万安县| 龙海市| 抚顺市| 临猗县| 新河县| 砚山县| 来凤县| 桦甸市| 军事| 开远市| 衡南县| 乐清市| 克什克腾旗| 时尚| 江油市| 阜平县|