用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - jsp代码库

上传

2012-10-14 作者: dota汤举报

[jsp]代码库

<%@ page contentType="text/html;charset=GBK" %>
<%@ page import="org.apache.commons.fileupload.servlet.ServletFileUpload" %>
<%@ page import="org.apache.commons.fileupload.FileItem" %>
<%@ page import="java.io.File" %>
<%@ page import="org.apache.commons.fileupload.disk.DiskFileItemFactory" %>
<%@ page import="org.apache.commons.fileupload.FileUpload" %>
<%@ page import="org.apache.commons.fileupload.servlet.ServletRequestContext" %>
<%@ page import="org.apache.commons.fileupload.FileUploadException" %>
<%@ page import="org.apache.commons.fileupload.RequestContext" %>
<%@ page import="java.util.*" %>
<%  String workItemID="";
    String affixnameTmp = "";
    String affixnameIdTmp = "";
    Date date = new Date();
    RequestContext requestContext = new ServletRequestContext(request);
    ////检查是否是一个文件上传请求 FileUpload.isMultipartContent(requestContext)
    if (FileUpload.isMultipartContent(requestContext)) {
        DiskFileItemFactory factory = new DiskFileItemFactory();
        factory.setRepository(new File("C://sunyard//sunflow230//Tomcat4//webapps//webench//tmp"));
        ServletFileUpload upload = new ServletFileUpload(factory);
        upload.setHeaderEncoding("gbk");
        upload.setSizeMax(10000000);  //10M
        List items = new ArrayList();
        try {
            items = upload.parseRequest(request);
        } catch (FileUploadException e1) {
            System.out.println("文件上传发生错误" + e1.getMessage());
        }
        Iterator it = items.iterator();
        while (it.hasNext()) {
            FileItem fileItem = (FileItem) it.next();
            if(fileItem.getFieldName().equalsIgnoreCase("workItemID")){
                 workItemID=new String(fileItem.getString().getBytes("iso8859-1"),"gbk");
            }
            if(fileItem.getFieldName().equalsIgnoreCase("affixname")){
                 affixnameTmp=new String(fileItem.getString().getBytes("iso8859-1"),"gbk");
            }
            if(fileItem.getFieldName().equalsIgnoreCase("affixnameId")){
                 affixnameIdTmp=new String(fileItem.getString().getBytes("iso8859-1"),"gbk");
            }
            if (fileItem.isFormField()) {
            } else {
                if (fileItem.getName() != null && fileItem.getSize() != 0) {
                    File fullFile = new File(fileItem.getName());
                    String affixname="",affixnameId="";
                    affixname = fullFile.getName();
                    int n = affixname.indexOf(".");
                    affixnameId = String.valueOf(date.getTime())+affixname.substring(n);
                    File newFile = new File("C:/sunyard/sunflow230/Tomcat4/webapps/webench/upload/" + affixnameId);
                    try {
                        fileItem.write(newFile);                      
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    affixnameTmp=affixnameTmp+affixname+";";
                     affixnameIdTmp=affixnameIdTmp+affixnameId+";";
                } else {
                    System.out.println("文件没有选择 或 文件内容为空");
                }
            }
        }
    }
//    response.sendRedirect("upload.jsp?&workItemID="+workItemID+"&affixnameId=" + affixnameIdTmp + "&affixname=" + affixnameTmp);
    out.println("<script>window.location='upload.jsp?&workItemID="+workItemID+"&affixnameId=" + affixnameIdTmp + "&affixname=" + affixnameTmp + "'</script>");
%>


网友评论    (发表评论)


发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...