tomcat 支持文件上传的功能实现

使用外部jar包: commons-io-2.5.jar 和 commons-fileupload-1.3.2.jar

表单代码:

    <form name="uploadForm" method="POST" action="servlet_6_url"  enctype="multipart/form-data">            //这里的enctype="multipart/form-data"一定不能掉了
      <table>
        <tr>
          <td><div align="right">excel文件:</div></td>
          <td><input type="file" name="upfileexcel"/></td>
        </tr>
        <tr>
          <td><div align="right">jpg文件:</div></td>
          <td><input type="file" name="upfilejpg"/></td>
        </tr>
        <tr>
          <td><input type="submit" name="submit" value="submit"/></td>
          <td><input type="reset" name="reset" value="reset"/></td>
        </tr>
      </table>
    </form>


tomcat代码:

package webpackage;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import org.apache.commons.fileupload.*;
import org.apache.commons.fileupload.servlet.*;
import org.apache.commons.fileupload.disk.*;
import java.util.*;


public class MyServlet_6 extends HttpServlet {
private long lMaxFileLength=400*1024*1024;//允许的最大文件大小
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, java.io.IOException {
// response.setContentType("text/html");
response.setContentType("text/html;charset=GB2312");
response.addHeader("Pragma", "no-cache");
response.setHeader("Cache-Control", "no-cache");
response.setHeader("Expires", "0");
PrintWriter pw=response.getWriter();
pw.println("<html><head><title>myservlet_6_title</title></head><body>");
pw.println("开始处理上传文件!<br>");
try{
DiskFileItemFactory dfit=new DiskFileItemFactory();
dfit.setSizeThreshold(4096);//设置缓冲区大小
String strFolderPath=getServletContext().getRealPath("/TempFile");
dfit.setRepository(new File(strFolderPath));
ServletFileUpload upload=new ServletFileUpload(dfit);
upload.setSizeMax(lMaxFileLength);//设置最大允许上传的文件大小  如果上传的文件超过这个大小,下面的代码会抛异常
List items=upload.parseRequest(request);
Iterator itor=items.iterator();
while(itor.hasNext()){
pw.println("处理一个新item<br>");
FileItem fi=(FileItem)itor.next();//如果上传的文件大小大于上面设定的文件大小,则本行代码会抛异常 the request was rejected because its size (157453870) exceeds the configured maximum (104857600)
if(fi.isFormField())
processFormField(fi,pw);
else
processUploadFile(fi,pw);
}
}
catch(Exception e){
pw.println("发现异常,信息是: "+e.getMessage()+"<br>");
}
pw.println("</body></html>");
pw.close();
}


@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, java.io.IOException {
doGet(request,response);
}

private void processFormField(FileItem fi,PrintWriter pr){
String strName=fi.getFieldName();
String strValue=fi.getString();
pr.println(strName+": "+strValue+"<br>");
}

private void processUploadFile(FileItem fi,PrintWriter pr){
try{
String strFileName=fi.getName();
strFileName=strFileName.substring(strFileName.lastIndexOf('\\')+1, strFileName.length());
long lFileSize=fi.getSize();
// if(lFileSize>lMaxFileLength){
// pr.println("【文件上传失败提示】: "+strFileName+" is large than "+lMaxFileLength+"B, upload failed !<br>");
// return;
// }
if(lFileSize==0||strFileName==null||strFileName.trim().length()==0)
return;
File flUpload=new File(getServletContext().getRealPath("/UploadFile")+"/"+strFileName);//这里竟然是正斜杠
fi.write(flUpload);
pr.println("【文件上传成功提示】: "+strFileName+" is saved. "+"Its size is "+lFileSize+"<br>");
}
catch(Exception e){
pr.println("保存上传文件时发现异常,信息是: "+e.getMessage()+"<br>");
System.out.println("发现异常: "+e.getMessage());
}
}
}







阅读更多
上一篇myeclipse下对tomcat项目进行debug断点调试
下一篇tomcat支持文件下载
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭