package jspSmartUpload;
import java.io.File;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;
public class UploadServlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("gbk");
resp.setCharacterEncoding("gbk");
String file = getServletContext().getRealPath("/")+"images";
File images = new File(file);
if(!images.exists()){
images.mkdirs();
}
System.out.println(file);
SmartUpload smart = new SmartUpload();
smart.initialize(getServletConfig(), req, resp);
smart.setMaxFileSize(1024*1024*1024);
smart.setAllowedFilesList("txt,jpg,gif,png");
try {
smart.upload();
} catch (Exception e) {
// TODO Auto-generated catch block
String mess = e.getMessage();
if(mess.indexOf("1010")!=-1)
{
System.out.println("文件不支持");
}
System.out.println(smart.getRequest().getParameter("text"));
for(int i = 0;i<smart.getFiles().getCount();i++){
com.jspsmart.upload.File tf = smart.getFiles().getFile(i);
System.out.println(tf.getFilePathName()+"getFilePathName");
System.out.println(tf.getFileName());
try {
tf.saveAs(file+"/123."+tf.getFileExt());
} catch (SmartUploadException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
req.setAttribute("name", "123.png");
}
req.getRequestDispatcher("index.jsp").forward(req, resp);
}
}
smartUpload上传
最新推荐文章于 2022-04-06 23:49:10 发布