一个简单的jQuery插件ajaxfileupload.js实现ajax上传文件例子

2020-05-19 07:27:52易采站长站整理

        <input type=”file” id=”houseMaps” name=”houseMaps”/>
        <input type=”button” value=”提交” onclick=”ajaxFileUpload()”/>
    </form>
    <div id=”result”></div>
   
  </body>
</html>

服务器代码:


public class UpdateAction extends DispatchAction {

    public ActionForward uploader(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) {
        UpFormForm upFormForm = (UpFormForm) form;
        FormFile ff = upFormForm.getHouseMaps();
        try {
            InputStream is = ff.getInputStream();
            File file = new File(“D:/” + ff.getFileName());            //指定文件存储的路径和文件名
            OutputStream os = new FileOutputStream(file);
           
            byte[] b = new byte[1024];
            int len = 0;
            while((len = is.read(b)) != -1){
                os.write(b, 0, len);
            }
            os.close();
            is.close();
        } catch (Exception e) {
            e.printStackTrace();
           
        }
       
        return null;
    }
}