java文件上传

java文件上传

适用于JSP,将jspSmartUpload.jar置于WEB-INF\lib下

----------------------------------------------------------

Jspsmart1.html

<html>
<head>
    <title>Jspsmart1.html</title>
<meta http-equiv="Content-Type" content="text/html; charset=GB2312">
</head>
<body>

<h2>文件上传范例 - jspSmart</h2>

<form name="Form1" enctype="multipart/form-data" method="post" action="Jspsmart1.jsp">
<p>上传文件 1:<input type="file" name="File1" size="20" maxlength="20"></p>
<input type="submit" value="上传">    
<input type="reset" value="清除">  
</form>

</body>
</html>

Jspsmart1.jsp

<%@ page import="com.jspsmart.upload.*" %>
<%@ page contentType="text/html;charset=GB2312" %>

<html>
<head>
    <title>Jspsmart1.jsp</title>
</head>
<body>

<h2>文件上传范例 - jspSmart</h2>

<jsp:useBean id="mySmartUpload" scope="page" class="com.jspsmart.upload.SmartUpload" />
<%
//计算文件上传个数
int count=0;         

//SmartUpload的初始化,使用这个jspsmart一定要在一开始就这样声明
mySmartUpload.initialize(pageContext);    

//生命限制上传的文件大小为 5 MB    
mySmartUpload.setMaxFileSize(5 * 1024 * 1024);

//依据form的内容上传
mySmartUpload.upload();

try {        
    //将文件存放于D:\totalExample\jsp\UploadFile\  
    count = mySmartUpload.save("D:\\totalExample\\jsp\\UploadFile\\");
  
    //打印出上传文件的个数   
    out.println("您成功上传"+count + "个文件.");
  
} catch (Exception e) {
    out.println(e.toString());
}
%>
</body>
</html>

---------------------------------------------------------------------

Jspsmart2.html

<html>
<head>
    <title>Jspsmart3.html</title>
<meta http-equiv="Content-Type" content="text/html; charset=GB2312">
</head>
<body>

<h2>文件上传范例 - jspSmart</h2>

<form name="Form1" enctype="multipart/form-data" method="post" action="Jspsmart2.jsp">
<p>上传文件 1:<input type="file" name="File1" size="20" maxlength="20"></p>
<input type="submit" value="上传">    
<input type="reset" value="清除">  
</form>

</body>
</html>

Jspsmart2.jsp

<%@ page import="com.jspsmart.upload.*" %>
<%@ page contentType="text/html;charset=GB2312" %>

<html>
<head>
    <title>Jspsmart2.jsp</title>
</head>
<body>

<h2>文件上传范例 - jspSmart</h2>

<jsp:useBean id="mySmartUpload" scope="page" class="com.jspsmart.upload.SmartUpload" />
<%
//计算文件上传个数
int count=0;

//SmartUpload的初始化,使用这个jspsmart一定要在一开始就这样声明
mySmartUpload.initialize(pageContext);

//依据form的内容上传
mySmartUpload.upload();

//将上传的文件一个一个取出来处理
for (int i=0;i<mySmartUpload.getFiles().getCount();i++)
{
       //取出一个文件
       com.jspsmart.upload.File myFile = mySmartUpload.getFiles().getFile(i);
  
       //如果文件存在,则做存档操作
       if (!myFile.isMissing()) {
  
           //将文件存放于绝对路径的位置
           myFile.saveAs("D:\\totalExample\\jsp\\UploadFile\\" + myFile.getFileName(), mySmartUpload.SAVE_PHYSICAL);
   
           //显示此上传文件的详细信息
           out.println("FieldName = " + myFile.getFieldName() + "<BR>");
           out.println("Size = " + myFile.getSize() + "<BR>");
           out.println("FileName = " + myFile.getFileName() + "<BR>");
           out.println("FileExt = " + myFile.getFileExt() + "<BR>");
           out.println("FilePathName = " + myFile.getFilePathName() + "<BR>");
           out.println("ContentType = " + myFile.getContentType() + "<BR>");
           out.println("ContentDisp = " + myFile.getContentDisp() +"<BR>");
           out.println("TypeMIME = " + myFile.getTypeMIME() +"<BR>");
           out.println("SubTypeMIME = " + myFile.getSubTypeMIME() + "<BR>");
           count ++;
       }
}

// 显示应该上传的文件数目
out.println("<BR>" + mySmartUpload.getFiles().getCount() + " files could be uploaded.<BR>");

// 显示成功上传的文件数目
out.println(count + "file(s) uploaded.");
%>

</body>
</html>

------------------------------------------------------------------------------

Jspsmart3.html

<html>
<head>
    <title>Jspsmart3.html</title>
<meta http-equiv="Content-Type" content="text/html; charset=GB2312">
</head>
<body>

<h2>文件上传范例 - jspSmart</h2>

<form name="Form1" enctype="multipart/form-data" method="post" action="Jspsmart2.jsp">
<p>上传文件 1:<input type="file" name="File1" size="20" maxlength="20"></p>
<input type="submit" value="上传">    
<input type="reset" value="清除">  
</form>

</body>
</html>

Jspsmart3.jsp

<%@ page import="com.jspsmart.upload.*" %>
<%@ page contentType="text/html;charset=GB2312" %>

<html>
<head>
    <title>Jspsmart3.jsp</title>
</head>
<body>

<h2>文件上传范例 - jspSmart</h2>

<jsp:useBean id="mySmartUpload" scope="page" class="com.jspsmart.upload.SmartUpload" />
<%

//计算文件上传个数
int count=0;         

//SmartUpload之初始化,使用这个jspsmart一定要在一开始就这样声明
mySmartUpload.initialize(pageContext);      

//声明可以上传的文件类型
mySmartUpload.setAllowedFilesList("htm,html,txt,,");

//限制存档位置,可存档于绝对位置
mySmartUpload.setDenyPhysicalPath(false);

//依据 form之内容上传
mySmartUpload.upload();

//将文件用原本的名字存放于server上的相对路径
try {
     count = mySmartUpload.save("D:\\totalExample\\jsp\\UploadFile\\", mySmartUpload.SAVE_PHYSICAL);
   
} catch (Exception e)    {

      out.println("<b>Wrong selection : </b>" + e.toString());
    }
     
//打印出总共上传文件个数
out.println(count + " file(s) uploaded.");
%>

</body>
</html>

---------------------------------------------------------------

download.jsp

<%@ page import="com.jspsmart.upload.*" %>
<%@ page contentType="text/html;charset=GB2312" %>

<html>
<head>
    <title> download.jsp</title>
</head>
<body>

<h2>文件下载范例 - jspSmart</h2>

<jsp:useBean id="mySmartUpload" scope="page"
                                                          class="com.jspsmart.upload.SmartUpload" />

<%
// SmartUpload之初始化
mySmartUpload.initialize(pageContext);

//必须如此声明,否则将会把文件显示于浏览器中
mySmartUpload.setContentDisposition("inline;");

//将 sample.zip下载,下载默认名称为downloaded.zip
mySmartUpload.downloadFile("C:\\upload\\sample.zip",
                                                    "application/x-zip-compressed",
                                                    "downloaded.zip");
%>

</body>
</html>



J-CN工作室
www.j-cn.org

你可能感兴趣的:(java文件上传)