怎样用servlet实现文件上传

 我来答
lzs丶灬
2016-12-08 · TA获得超过632个赞
知道小有建树答主
回答量:881
采纳率:100%
帮助的人:505万
展开全部
/**
     * 文件上传处理主程序。
     * 
     * @return int 操作结果 0 文件操作成功;1 request对象不存在。 2 没有设定文件保存路径或者文件保存路径不正确;3
     *         没有设定正确的enctype;4 文件操作异常。
     */
    public Map<String, String> fileupload_java(HttpServletRequest request,String uploadpath) {
     Map<String, String> param = new HashMap<String, String>();
    
     try {
            // 参数或者文件名
            String name = null;
            // 参数的value
            String value = null;
            // 读取的流是否为文件的标志位
            boolean fileFlag = false;
            // 要存储的文件。
            File tmpFile = null;
            // 上传的文件的名字
            String fName = null;
            FileOutputStream baos = null;
            BufferedOutputStream bos = null;
            int rtnPos = 0;
            byte[] buffs = new byte[BUFSIZE * 8];
            
            // 取得ContentType
            String contentType = request.getContentType();
            int index = contentType.indexOf("boundary=");
            String boundary = "--" + contentType.substring(index + 9);
            String endBoundary = boundary + "--";
            
            // 从request对象中取得流。
            ServletInputStream sis = request.getInputStream();
            // 读取1行
            while ((rtnPos = sis.readLine(buffs, 0, buffs.length)) != -1) {
            
                String strBuff = new String(buffs, 0, rtnPos);
                if (strBuff.startsWith(boundary)) {
                    if (name != null && name.trim().length() > 0) {
                        if (fileFlag) {
                            bos.flush();
                            baos.close();
                            bos.close();
                            baos = null;
                            bos = null;
                            param.put(name, tmpFile.getAbsolutePath());
                        } else {
                            String paramValue = param.get(name);
                            paramValue += ","+ value;
                            param.put(name, paramValue);
                        }
                    }
                    name = new String();
                    value = new String();
                    fileFlag = false;
                    fName = new String();
                    rtnPos = sis.readLine(buffs, 0, buffs.length);
                    if (rtnPos != -1) {
                        strBuff = new String(buffs, 0, rtnPos);
                        if (strBuff.toLowerCase().startsWith("content-disposition: form-data; ")) {
                            int nIndex = strBuff.toLowerCase().indexOf("name=\"");
                            int nLastIndex = strBuff.toLowerCase().indexOf("\"", nIndex + 6);
                            name = strBuff.substring(nIndex + 6, nLastIndex);
                        }
                        int fIndex = strBuff.toLowerCase().indexOf("filename=\"");
                        if (fIndex != -1) {
                            fileFlag = true;
                            int fLastIndex = strBuff.toLowerCase().indexOf("\"", fIndex + 10);
//                            fName = strBuff.substring(fIndex + 10, fLastIndex);
                            fName = new String(strBuff.substring(fIndex + 10, fLastIndex).getBytes(),"gbk");
                            fName = FileL.getFileNameWithoutSeprater(fName);
                            if (fName == null || fName.trim().length() == 0) {
                                fileFlag = false;
                                sis.readLine(buffs, 0, buffs.length);
                                sis.readLine(buffs, 0, buffs.length);
                                sis.readLine(buffs, 0, buffs.length);
                                continue;
                            }else{
                                fName = FileL.getFileNameTime(fName);
                                sis.readLine(buffs, 0, buffs.length);
                                sis.readLine(buffs, 0, buffs.length);
                            }
                        }
                    }
                } else if (strBuff.startsWith(endBoundary)) {
                 if (name != null && name.trim().length() > 0) {
                        if (fileFlag) {
                            bos.flush();
                            baos.close();
                            bos.close();
                            baos = null;
                            bos = null;
                            param.put(name, tmpFile.getAbsolutePath());
                        } else {
                            String paramValue = param.get(name);
                            paramValue += ","+ value;
                            param.put(name, paramValue);
                        }
                    }
                } else {
                    if (fileFlag) {
                        if (baos == null && bos == null) {
                            tmpFile = new File(uploadpath + fName);
                            baos = new FileOutputStream(tmpFile);
                            bos = new BufferedOutputStream(baos);
                        }
                        bos.write(buffs, 0, rtnPos);
                        baos.flush();
                    } else {
                        value = value + strBuff;
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return param;
    }
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式