文章目录

在java中要实现异步上传要提前做好准备,对于文件上传,浏览器在上传的过程中是将文件以流的形式提交到服务器端的,如果直接使用Servlet获取上传文件的输入流然后再解析里面的请求参数是比较麻烦,所以一般选择采用apache的开源工具common-fileupload这个文件上传组件。

这个common-fileupload上传组件的jar包可以去apache官网上面下载,也可以在struts的lib文件夹下面找到,struts上传的功能就是基于这个实现的。

common-fileupload是依赖于common-io这个包的,所以还需要下载这个包。剩下的就是js文件的导入了,我导入了以下文件:

<script type="text/javascript" src="lib/Js/jquery.js"></script>
 <script ltype="text/javascript" src="/js/ajaxfileupload.js"></script>

在页面中的写法:

 div class="controls">
<span class="btn green fileinput-button">
<i class="icon-plus icon-white"></i>
    <span>上传照片</span>
    <input id="fileToUpload" name="myfiles" type="file" onchange="upload()" title="上传" />
</span>
 </div>

function upload(){
            $.ajaxFileUpload  
             (  
                 {  
                     url:'<%=basePath%>sysperson/uploadpic',  
                     secureuri:false,  
                     fileElementId:'fileToUpload',  
                     dataType: 'text',  
                     success: function (data, status)  
                     {
                        document.all.mypic.src="<%=basePath%>uploads/" + data;
                        document.all.picpath.value = data;
                    },
                    error : function(data, status, e) {
                        alert(e);
                    }
                });
            }

java代码写法:

@RequestMapping(value = "uploadpic",method=RequestMethod.POST)
    public void fileUpload(@RequestParam MultipartFile[] myfiles,
            HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        String imgPath = "/uploads" + "/";
        File directory = new File(request.getSession().getServletContext()
                .getRealPath("/")
                + imgPath);
        String desFileName = null;
        String fileNewName = null;
        response.setContentType("text/html; charset=UTF-8");
        PrintWriter out = response.getWriter();
        String originalFilename = null;
        for (MultipartFile myfile : myfiles) {
            if (myfile.isEmpty()) {
                out.write("请选择文件后上传");
                out.flush();
            } else {
                originalFilename = myfile.getOriginalFilename();
                if (null != originalFilename && originalFilename.length() > 0) {
                    fileNewName = UUID.randomUUID() + originalFilename;
                    desFileName = directory.toString() + "/" + fileNewName;
                }
                try {
                    FileUtils.copyInputStreamToFile(myfile.getInputStream(),
                            new File(desFileName));
                } catch (IOException e) {
                    e.printStackTrace();
                    out.write("文件上传失败,请重试!!");
                    out.flush();
                }
            }
        }
        out.print(fileNewName);
        out.flush();
    }


本文出处程序员头条:http://www.iswifting.com/2015/12/15/java-upload-picture/
转载请在开头注明本文出处。

欢迎关注本站微信公众号:为程序员提供最优质的博文、最精彩的讨论、最实用的开发资源;提供最新最全的编程学习资料:PHP、Objective-C、Java、Swift、C/C++函数库、.NET Framework类库、J2SE API等等.并不定期奉送各种福利.
微信公众号猿圈:CodePush

文章目录