java文件上传,Struts2文件上传,demo下载

阅读更多
闲话不说,下载附件吧,可以运行的demo

简单的介绍下,附件中一个是基于smartupload的文件上传,另一个是struts2的文件上传。

例一核心代码如下:
package com.uploadfile;

import java.io.IOException;

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

import com.jspsmart.upload.File;
import com.jspsmart.upload.SmartUpload;

public class UploadfileServlet extends HttpServlet{

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

		doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try {
			// 上传后图片存储的位置。
			ServletContext application = this.getServletContext();
			String filePath = "d:/upload";
			System.out.println(filePath);
			// 对本地文件和目录抽象表示。
			java.io.File file = new java.io.File(filePath);
			// 判断文件或目录是否存在。
			if (!file.exists()) {
				file.mkdir();// 目录不存在的情况下,创建目录。
			}

			// 做文件上传的基础类,里面有关于文件上传的方法。
			SmartUpload mysmart = new SmartUpload();
			// 初始化。
			mysmart.initialize(this.getServletConfig(), request, response);

			// 上传.
			mysmart.upload();
			// 循环取到我要上传的每一个文件。
			for (int i = 0; i < mysmart.getFiles().getCount(); i++) {
				// 取到其中一个
				File myfile = mysmart.getFiles().getFile(i);
				// 判断文件是否丢失。
				if (!myfile.isMissing()) {// 没有丢失
					// 保存文件
					myfile.saveAs(filePath + "/" + myfile.getFileName(),
							File.SAVEAS_PHYSICAL);
				}
			}
			System.out.println("上传成功");
		} catch (Exception e) {
			System.out.println("上传失败!");
		}

		// 返回登录成功,显示头像。
		request.getRequestDispatcher("/success.jsp").forward(request, response);

	}

}

例二核心代码如下:
package com.uploadfile.action;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class UploadFileAction extends ActionSupport{
	
	private static final long serialVersionUID = 572146812454l;
	private static final int BUFFER_SIZE = 16 * 1024;

	private File myFile;
	private String contentType;
	private String fileName;
	private String filePath;
	
	

	public String getFilePath() {
		return filePath;
	}

	public void setFilePath(String filePath) {
		this.filePath = filePath;
	}

	public void setMyFileContentType(String contentType) {
		this.contentType = contentType;
	}

	public void setMyFileFileName(String fileName) {
		this.fileName = fileName;
	}

	public void setMyFile(File myFile) {
		this.myFile = myFile;
	}

	public String getContentType() {
		return contentType;
	}

	public String getFileName() {
		return fileName;
	}
	
	public File getMyFile() {
		return myFile;
	}

	private static void copy(File src, File dst) {
		try {
			InputStream in = null;
			OutputStream out = null;
			try {
				in = new BufferedInputStream(new FileInputStream(src),
						BUFFER_SIZE);
				out = new BufferedOutputStream(new FileOutputStream(dst),
						BUFFER_SIZE);
				byte[] buffer = new byte[BUFFER_SIZE];
				while (in.read(buffer) > 0) {
					out.write(buffer);
				}
			} finally {
				if (null != in) {
					in.close();
				}
				if (null != out) {
					out.close();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public String uploadFileByStruts2(){
		File file1 = new File(filePath);
		// 判断文件或目录是否存在。
		if (!file1.exists()) {
			file1.mkdir();// 目录不存在的情况下,创建目录。
		}
		System.out.println(fileName);
		System.out.println(contentType);
		File file = new File(filePath+"/"+fileName);
		copy(myFile, file);
		System.out.println("执行结束"+filePath);
		return "success";
	}

}
  • TestUpload.zip (20.4 KB)
  • 下载次数: 68
  • TestUpload_Struts2.zip (3.4 MB)
  • 下载次数: 185

你可能感兴趣的:(java文件上传,Struts2文件上传,demo下载)