我写了个上传文件的程序,代码如下:
在红色的地方,出现问题,没有得到值,为空。麻烦各位帮我看看是那出了问题,我实在是找不出问题了谢谢指教
private String uploadPath = "D:\\temp"; // 上传文件的目录
private String tempPath = "D:\\"; // 临时文件目录
File tempPathFile;
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
try {
DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setSizeThreshold(4096); // 设置缓冲区大小,这里是4kb
factory.setRepository(tempPathFile);// 设置缓冲区目录
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setSizeMax(4194304); // 设置最大文件尺寸,这里是4MB
List<FileItem> items = upload.parseRequest(request);// 得到所有的文件 System.out.println(items.size()+"fff");
Iterator<FileItem> i = items.iterator();
while (i.hasNext()) {
FileItem fi = (FileItem) i.next();
String fileName = fi.getName();
if (fileName != null) {
File fullFile = new File(fi.getName());
File savedFile = new File(uploadPath, fullFile.getName());
fi.write(savedFile);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}<%@ page language="java" pageEncoding="GBK"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@page import="java.util.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title></title>
</head>
<body>
<form name="myform" action="Upload" method="post" enctype="multipart/form-data">
<input type="file" name="myfile"/>
<input type="submit" value="导入" />
</form>
</body>
</html>

解决方案 »

  1.   

    楼主这个很麻烦 建议用个jar包就可以了
      

  2.   

    用 SmartUpload 这个就可以轻松实现很简单的
      

  3.   

    用什么好点的jar包吗?麻烦赐教
      

  4.   

    SmartUpload我目前只学过这个 感觉很好用!
      

  5.   

    用smratupload做!
    <%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
    <jsp:useBean id="smartupload" class="org.lxh.smart.SmartUpload"/>
    <html>
    <head>
    <title>smartupload</title>
    </head>
    <body>
    <%
        //String name= request.getParameter("username");
        //因为form为数据封装了所以,request是不能接到参数的
        request.setCharacterEncoding("GBK");
    smartupload.initialize(pageContext) ; // 初始化上传
    smartupload.upload() ; // 准备上传
    String name =smartupload.getRequest().getParameter("username");
    String fileName =this.getServletContext().getRealPath("/")+"/upload"+name;
    smartupload.getFiles().getFile(0).saveAs(fileName); // 保存文件
    %>
    <h1><%=name%></h1>
    <img src="upload/<%=name%>" width="300" height="200">
    </body>
    </html>
      

  6.   

    smratupload就是楼上说的jar包一种。你可以试试。
      

  7.   

    我想问一下为什么我在csdn上面下载的超市管理信息系统rar文件解压后不能运行,谁能告诉我应该运行哪个文件 马上交作业了 愁的慌  会的可以加我QQ:357550193 在线等 急急急!!!!!
      

  8.   

    LZ那个用什么实现的,都没看懂!汗。。我只用纯jsp写过!
      

  9.   


    org.apache.commons.fileupload这个做的啊