1. 程式人生 > >springmvc圖片上傳(兼容ie8以上,實時預覽)

springmvc圖片上傳(兼容ie8以上,實時預覽)

處理 spa aps news time htm tip 技術分享 ans

html代碼:

技術分享
<form id="uploadform" method="post" enctype="multipart/form-data">
            <table>
                <tr>
                    <td><span class="need">&nbsp;&nbsp;&nbsp;</span>新聞圖片:</td>
                    <td width="100" align="right"><
input type="file" onchange="preview(this)" name="newsImages" id="newsImages" /></td> </tr> <tr> <td colspan="2"> <div id="preview"></div> </td>
</tr> </table> </form>
View Code

js:

技術分享
<script type="text/javascript" src="resource/js/jquery-1.9.1.min.js"></script>
<script src="http://malsup.github.io/jquery.form.js"></script>
function preview(file) {
            var prevDiv = document.getElementById(‘preview‘);
            
if (file.files && file.files[0]) { var reader = new FileReader(); reader.onload = function(evt) { prevDiv.innerHTML = ‘<img src="‘ + evt.target.result + ‘" />‘; } reader.readAsDataURL(file.files[0]); } else { prevDiv.innerHTML = ‘<div class="img" style="filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale,src=\‘‘ + file.value + ‘\‘"></div>‘; } uploadPhoto(); } function uploadPhoto() { var imagePath = $("#newsImages").val(); if (imagePath == "") { alert("please upload image file"); return false; } var strExtension = imagePath.substr(imagePath.lastIndexOf(‘.‘) + 1); if (strExtension != ‘jpg‘ && strExtension != ‘gif‘ && strExtension != ‘png‘ && strExtension != ‘bmp‘) { alert("please upload file that is a image"); return false; } $("#uploadform").ajaxSubmit({ type : ‘POST‘, url : ‘news/upload‘, data : { imgPath : $("#newsImages").val() }, success : function(data) { $("input[name=‘newsImage‘]").val(data); }, error : function() { alert("上傳失敗,請檢查網絡後重試"); } }); }
View Code

controller:

技術分享
@Controller
@RequestMapping(value = "/news")
public class NewsController {

/**
     * 處理上傳的file文件的圖片
     * @author zhangyn
     * @param map
     * @param request
     * @param newsImages
     * @return
     */
    @RequestMapping(value = "/upload")
    @ResponseBody
    public String upload(ModelMap map, HttpServletRequest request,
            @RequestParam(value = "newsImages", required = false) MultipartFile newsImages) {
        String pic_path = request.getSession().getServletContext().getRealPath("/")+"upload";//保存在項目下的upload文件夾下。
        String fileName = newsImages.getOriginalFilename();
        Date date=new Date();
        long time = date.getTime();
        File targetFile = new File(pic_path, time+fileName);//重命名的考慮是按照一定的格式存儲利於查找,且避免了相同名稱的覆蓋。
        if (!targetFile.exists()) {
            targetFile.mkdirs();
        }
        try {
            newsImages.transferTo(targetFile);
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    
        return "upload/"+time+fileName;
    }
}
View Code

spring-mvc.xml

技術分享
    <!-- 上傳文件攔截,設置最大上傳文件大小 10M=10*1024*1024(B)=10485760 bytes -->
    <bean id="multipartResolver"
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="maxUploadSize" value="10485760" />
    </bean>
View Code

springmvc圖片上傳(兼容ie8以上,實時預覽)