⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 processupload.java

📁 jsp中文件上传和下载
💻 JAVA
字号:
package com.buat;

import java.io.IOException;
import java.sql.Date;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.jspsmart.upload.File;
import com.jspsmart.upload.Files;
import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;

public class ProcessUpload extends HttpServlet {
	private ServletConfig config;

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

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

		// 前期准备获取数据
		request.setCharacterEncoding("gbk");

		// Define a flag indicating whether upload is successful.
		boolean successful = false;
		// 1.Define a SmartUpload component.
		SmartUpload mySmartUpload = new SmartUpload();
		// 2.initialize the upload component by using config parameter.
		mySmartUpload.initialize(config, request, response);
		// 准备工作
		try {
			// 3.logic upload ,get all the files used to be uploaded.
			mySmartUpload.upload();
			Files files = mySmartUpload.getFiles();

			for (int i = 0; i < files.getCount(); i++) {

				File myFile = files.getFile(i);

				// 4.(Physical upload)upload the file content to the server,you
				// may specify the path types:absolute or relative.
				myFile.saveAs("/document/" + myFile.getFileName()+"(j2ee5)",
						mySmartUpload.SAVE_VIRTUAL);

				// 5.test the file attributes.
				System.out.println("FieldName = " + myFile.getFieldName());
				System.out.println("Size = " + myFile.getSize());
				System.out.println("FileName = " + myFile.getFileName());
				System.out.println("FileExt = " + myFile.getFileExt());
				System.out
						.println("FilePathName = " + myFile.getFilePathName());
				System.out.println("ContentType = " + myFile.getContentType());
				System.out.println("ContentDisp = " + myFile.getContentDisp());
				System.out.println("TypeMIME = " + myFile.getTypeMIME());
				System.out.println("SubTypeMIME = " + myFile.getSubTypeMIME());

			}

			successful = true;

		} catch (SmartUploadException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// 转发到原来页面
		String url = null;
		url = "/UploadResult.jsp";
		request.setAttribute("successful", successful);
		RequestDispatcher disPather = null;
		disPather = request.getRequestDispatcher(url);
		disPather.forward(request, response);
	}

	public void init(ServletConfig config) throws ServletException {
		// get the config used as uploading parameter.
		this.config = config;
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -