smartUpload上传

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);
	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值