web端 圖片上傳

來源:互聯網
上載者:User

標籤:

需要外掛程式:ajaxfileupload.js  commons-fileupload-1.3.1.jar
web端:
  

<form id="saveForm" method="post" enctype="multipart/form-data"><label>廣告圖片:</label><input  type="file" id="image1" name="image1" data-options="required:true" />                             <img src="../../imgSrc/aaaaaa.jpg" id="img" width="100px",height="100px"/>                              <input type="button" id="sub" value="上傳"/></form>
<script type="text/javascript">$(function(){    $("#sub").click(function(){        $.ajaxFileUpload({              url:‘upload.do‘,              secureuri:false,              fileElementId:‘image1‘,//file標籤的id              dataType: ‘json‘,//返回資料的類型              success: function (data) {                  $("#img").attr("src","../../imgSrc/"+data);            },          });    });});</script>

java後台:
  

    @RequestMapping("upload")    @ResponseBody    public  String upload(@RequestParam("image1") MultipartFile file,HttpServletRequest req){//         String saveFilePath = req.getServletContext().getRealPath("/WEB-INF/upload");        String saveFilePath=Constant.savePath;        try {            if (!file.isEmpty()) {                 // 擷取圖片的檔案名稱                String fileName = file.getOriginalFilename();                // 擷取圖片的副檔名                String extensionName = fileName                        .substring(fileName.lastIndexOf(".") + 1);                // 新的圖片檔案名稱 = 擷取時間戳記+"."圖片副檔名                String newFileName = String.valueOf(System.currentTimeMillis()) + "." + extensionName;                ImgUtils.saveFile(newFileName, file, saveFilePath);                     return newFileName;              }else{                 return "exception";            }    } catch (Exception e) {        e.printStackTrace();        return "Exception";    }     }
    public static void saveFile(String newFileName, MultipartFile filedata,String saveFilePath) {        // TODO Auto-generated method stub        // 根據設定檔擷取伺服器圖片存放路徑        /* 構建檔案目錄 */        File fileDir = new File(saveFilePath);        if (!fileDir.exists()) {            fileDir.mkdirs();        }        try {            FileOutputStream out = new FileOutputStream(saveFilePath + "\\"                    + newFileName);            // 寫入檔案            out.write(filedata.getBytes());            out.flush();            out.close();        } catch (Exception e) {            e.printStackTrace();        }     }

tomcat:
  

      <Host appBase="webapps" autoDeploy="true" name="localhost" unpackWARs="true">        <Context docBase="D:\upload" path="/imgSrc"/>

 

web端 圖片上傳

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.