日期:2014-05-16  浏览次数:20415 次

JSP实现文件的上传
<form action="AddFilePath" method="post" enctype="multipart/form-data" >
<input type="file" name="X" />
<input type="submit" value="上传"/> 
</form>

?

package test.action;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.lxh.smart.SmartUpload;
import org.lxh.smart.SmartUploadException;

import test.dao.FilePathDAO;
import test.model.FilePath;

public class AddFilePath extends HttpServlet {

	private static final long serialVersionUID = 2670752504675877211L;

	public AddFilePath() {
		super();
	}

	public void destroy() {
		super.destroy();

	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
        
		SmartUpload su=new SmartUpload();
		su.initialize(this.getServletConfig(), request, response);
		try {
			su.upload();
			su.save("/123/");
		} catch (SmartUploadException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		int i=1;
		String filename=su.getFiles().getFile(0).getFileName();
	    String filepath="123/"+filename;
	    
	    FilePath fpd=new FilePath();
	    fpd.setFile_id(i);
	    fpd.setFile_name(filename);
	    fpd.setFile_path(filepath);
	    FilePathDAO fpd2=new FilePathDAO();
	    fpd2.addFilePath(fpd);
	   
		
	}

	public void init() throws ServletException {
	}
}

?

package test.model;

import java.util.Date;

public class FilePath {
    private int file_id;              //文件ID
    private String file_name;         //文件名称
    private String file_path;         //文件路径
	public int getFile_id() {
		return file_id;
	}
	public void setFile_id(int fileId) {
		file_id = fileId;
	}
	public String getFile_name() {
		return file_name;
	}
	public void setFile_name(String fileName) {
		file_name = fileName;
	}
	public String getFile_path() {
		return file_path;
	}
	public void setFile_path(String filePath) {
		file_path = filePath;
		
		Date date=new Date();
	}
    
}

?

上面的代码没有 用? request.getPermeter();的方法从页面取值,所以这样的方法可能不奏效。解决页面取不到值的方法是。String s = su.getRequest().getParameter();

这样的方式取值