spring mvc框架+ ajax實現 文件上傳


1.前端頁面,通過form表單提交,必須設置

enctype="multipart/form-data"  代表form表單在發送到服務器時候編碼方式是二進制類型,一般用於圖片、mp3、文件
<form method="get" action="" class="form-horizontal" role="form" id="form_data" onsubmit="return check_form()" style="margin: 20px;" enctype="multipart/form-data">
                    <div class="modal fade" id="addModal" tabindex="-1" role="dialog"  aria-labelledby="myModalLabel" aria-hidden="true">
                        <div class="modal-dialog">
                            <div class="modal-content">
                                <div class="modal-header">
                                    <button type="button" class="close" data-dismiss="modal"  aria-hidden="true">&times;</button>
                                    <h4 class="modal-title" id="myModalLabel">配置信息</h4>
                                </div>
                                <div class="modal-body">
                                    <form class="form-horizontal" role="form">
                                        <div class="form-group">
                                            <label for="projectid" class="col-sm-3 control-label">項目名稱</label>
                                            <div class="col-sm-9">
                                                <select class="form-control" name="projectid" id="projectid">
                                                    <c:forEach var="p" items="${projects }">
                                                        <option value="${p.projectid}">${p.projectname}</option>
                                                    </c:forEach>
                                                </select>
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label for="apppackage" class="col-sm-3 control-label">app包名</label>
                                            <div class="col-sm-9">
                                                <input type="text" class="form-control" name="apppackage" id="apppackage" placeholder="app包名">
                                            </div>
                                        </div>

                                        <div class="form-group">
                                            <%--@declare id="files"--%>
                                                <label for="files" class="col-sm-3 control-label">上傳apk</label>
                                            <div class="col-sm-9">
                                                <input type="file" class="form-control" id="apkupload" name="files">
                                            </div>
                                        </div>

                                        <div class="form-group">
                                            <label for="remark" class="col-sm-3 control-label">備注</label>
                                            <div class="col-sm-9">
                                                <textarea class="form-control" name="remark" id="remark"  placeholder="備注"></textarea>
                                            </div>
                                        </div>
                                    </form>
                                </div>
                                <div class="modal-footer">
                                    <button type="button" class="btn btn-default"   data-dismiss="modal" id="close">關閉</button>
                                    <button type="submit" class="btn btn-primary" disabled="disabled">提交</button>
                                    &nbsp;&nbsp;&nbsp;&nbsp;<span id="tip"> </span>
                                </div>
                            </div>
                        </div>
                    </div>
                </form>        

2. ajax請求 

注意:contentType要設置為false,
當在form 標簽中設置了enctype = “multipart/form-data”后,請求中的 contentType 會默認為 multipart/form-data 。
而ajax中contentType設置為false是為了避免 JQuery 對其操作,從而失去分界符,而使服務器不能正常解析文件(文件是需要分隔符的,比如文件內容和文本內容之間就是用分割符分割)
補充:
async 默認是true,即為異步方式,$.Ajax執行后,會繼續執行ajax后面的腳本,直到服務器端返回數據后,觸發$.Ajax里的success方法,這時候執行的是兩個線程。
      若為false,則所有的請求均為同步請求,用戶必須等待請求完成才能執行其他操作。
// 提交表單
    function check_form(){
        // var form_data = $('#form_data').serialize();
        var formData = new FormData($( "#form_data" )[0]);
        // 異步提交數據到action頁面
        $.ajax({
                url: "configadd.do",
                data:formData,
                type: "post",
                // dataType : "json",//返回數據的類型
                async: false,
                cache: false,
                contentType: false,//發送數據的類型
                processData: false,
                beforeSend:function(){
                    $("#tip").html("<span style='color:blue'>正在處理...</span>");
                    return true;
                },
                success:function(data, status){
                    console.log("請求成功返回數據============="+status);
                    if(status == "success"){
                        alert("添加成功-------")
                        $("#tip").html("<span style='color:blueviolet'>恭喜,添加配置成功!</span>");
                        setTimeout(fn, 1000);
                        function  fn(){
                            document.getElementById("close").click();
                        }
                        toastr.success(data.ms);
                    }else{
                        $("#tip").html("<span style='color:red'>失敗,請重試</span>");
                        toastr.info(data.ms);
                    }
                },
                error:function(){
                    toastr.error('請求出錯!');
                },
                complete:function(){
                    $('#addModal').hide();
                }
            });
        return false;
    }
3.Spring配置文件applicationContext.xml 中增加上傳文件的配置
    <!--文件上傳使用, 配置multipartResolver,id名為約定好的 -->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <!-- 配置文件(每次上傳的所有文件總大小)大小,單位為b, 1024000表示1000kb  -->
        <property name="maxUploadSize" value="102400000" />
    </bean>
    <!--PropertiesFactoryBean對properties文件可用 ,可以用來注入properties配置文件的信息 -->
    <bean id="uploadProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
        <property name="location" value="classpath:upload.properties"></property>
    </bean>
 
        

4.文件上傳的工具類

(一)上傳到服務器

@Component(value="fileUploadUtils")
public class FileUploadUtil {
    @Value("#{uploadProperties.path}")
    private String path;
    private String getExtName(MultipartFile file){
        return FilenameUtils.getExtension(file.getOriginalFilename());
    }
    private String createNewName(MultipartFile file){
        return UUID.randomUUID().toString()+"."+getExtName(file);
    }
    public String uploadFile(MultipartFile file){
        try {
            String newName=createNewName(file);
            FileUtils.copyInputStreamToFile(file.getInputStream(), new File(path,newName ));
            return newName;
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }
}

(二)通過ftp遠程上傳

public class FtpUtil {
    /**
     * Description: 向FTP服務器上傳文件
     * @param host FTP服務器hostname
     * @param port FTP服務器端口
     * @param username FTP登錄賬號
     * @param password FTP登錄密碼
     * @param basePath FTP服務器基礎目錄
     * @param filePath FTP服務器文件存放路徑。例如分日期存放:/2015/01/01。文件的路徑為basePath+filePath
     * @param filename 上傳到FTP服務器上的文件名
     * @param input 輸入流
     * @return 成功返回true,否則返回false
     */
    public static boolean uploadFile(String host, int port, String username, String password, String basePath,
                                     String filePath, String filename, InputStream input) {
        boolean result = false;
        FTPClient ftp = new FTPClient();
        try {
            int reply;
            ftp.connect(host, port);// 連接FTP服務器
            // 如果采用默認端口,可以使用ftp.connect(host)的方式直接連接FTP服務器
            ftp.login(username, password);// 登錄
            reply = ftp.getReplyCode();
            if (!FTPReply.isPositiveCompletion(reply)) {
                ftp.disconnect();
                return result;
            }
            //切換到上傳目錄
            if (!ftp.changeWorkingDirectory(basePath+filePath)) {
                //如果目錄不存在創建目錄
                String[] dirs = filePath.split("/");
                String tempPath = basePath;
                for (String dir : dirs) {
                    if (null == dir || "".equals(dir)) continue;
                    tempPath += "/" + dir;
                    if (!ftp.changeWorkingDirectory(tempPath)) {
                        if (!ftp.makeDirectory(tempPath)) {
                            return result;
                        } else {
                            ftp.changeWorkingDirectory(tempPath);
                        }
                    }
                }
            }
            //設置上傳文件的類型為二進制類型
            ftp.setFileType(FTP.BINARY_FILE_TYPE);
            //上傳文件
            if (!ftp.storeFile(filename, input)) {
                return result;
            }
            input.close();
            ftp.logout();
            result = true;
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (ftp.isConnected()) {
                try {
                    ftp.disconnect();
                } catch (IOException ioe) {
                }
            }
        }
        return result;
    }

    /**
     * Description: 從FTP服務器下載文件
     * @param host FTP服務器hostname
     * @param port FTP服務器端口
     * @param username FTP登錄賬號
     * @param password FTP登錄密碼
     * @param remotePath FTP服務器上的相對路徑
     * @param fileName 要下載的文件名
     * @param localPath 下載后保存到本地的路徑
     * @return
     */
    public static boolean downloadFile(String host, int port, String username, String password, String remotePath,
                                       String fileName, String localPath) {
        boolean result = false;
        FTPClient ftp = new FTPClient();
        try {
            int reply;
            ftp.connect(host, port);
            // 如果采用默認端口,可以使用ftp.connect(host)的方式直接連接FTP服務器
            ftp.login(username, password);// 登錄
            reply = ftp.getReplyCode();
            if (!FTPReply.isPositiveCompletion(reply)) {
                ftp.disconnect();
                return result;
            }
            ftp.changeWorkingDirectory(remotePath);// 轉移到FTP服務器目錄
            FTPFile[] fs = ftp.listFiles();
            for (FTPFile ff : fs) {
                if (ff.getName().equals(fileName)) {
                    File localFile = new File(localPath + "/" + ff.getName());

                    OutputStream is = new FileOutputStream(localFile);
                    ftp.retrieveFile(ff.getName(), is);
                    is.close();
                }
            }

            ftp.logout();
            result = true;
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (ftp.isConnected()) {
                try {
                    ftp.disconnect();
                } catch (IOException ioe) {
                }
            }
        }
        return result;
    }
    public static void main(String[] args) {
        try {
            FileInputStream in=new FileInputStream(new File("D:\\temp\\image\\gaigeming.jpg"));
            boolean flag = uploadFile("192.168.25.133", 21, "ftpuser", "ftpuser", "/home/ftpuser/www/images","/2015/01/21", "gaigeming.jpg", in);
            System.out.println(flag);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
}

5.實體AppiumConfig中增加文件類型的屬性

    private MultipartFile[] files;

6.控制層controller中,實現上傳功能,保存上傳文件的路徑到實體

                    MultipartFile[] files = AppiumConfig.getFiles();
//                    System.out.println("真實路徑:" + application.getRealPath("/"));
                    String path =application.getRealPath("/");
                    for (MultipartFile multipartFile : files) {
//                        System.out.println("文件類型:" + multipartFile.getContentType());
//                        System.out.println("文件控件名:" + multipartFile.getName());
//                        System.out.println("文件名:" + multipartFile.getOriginalFilename());
//                        System.out.println("文件大小:" + multipartFile.getSize());
                        try {
                            FileUtils.copyInputStreamToFile(multipartFile.getInputStream(), new File(application.getRealPath("/") + "upload/" + multipartFile.getOriginalFilename()));
                        } catch (IOException e) {
                            e.printStackTrace();
                        }   path +=multipartFile.getOriginalFilename();
                    }
                    AppiumConfig.setApkupload(path);

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM