用JSP写没问题,一转到servlet就上传不了了
第一个文件:show.jsp
<%@ page language="java" contentType="text/html;charset=gb2312" pageEncoding="GBK"%>
<html>
<body>
选择文件:<input type="File" name="picture">
<form action="upload" method="post" ENCTYPE="multipart/form-data"> //这里提交到下面的servlet
<input type="submit" value="上传">
</form>
</body>
</html>
第二个文件:
package com.yj;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.jspsmart.*;
import com.jspsmart.upload.*;
public class Upload extends HttpServlet {
public Upload() {
super();
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=gbk");
PrintWriter out = response.getWriter();
//新建一个SmartUpload对象
SmartUpload su = new SmartUpload();
int count = 0;
try {
//上传初始化
su.initialize(getServletConfig(),request,response);
//上传文件
su.upload();
count = su.save("/upload");//这里的文件夹存在,我把路径瞎改了它也不抛异常,奇了怪了
} catch (Exception e) {
e.printStackTrace();
}
out.println(count+"个文件上传成功");
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
}
最后显示0个文件上传成功,大家帮忙看看