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

📄 testupload.java

📁 用于上传的原码。上传(j2ee包太大你自己找一个放到这里就可以了).rar
💻 JAVA
字号:
package com.lfs;

import java.io.File;

import javax.servlet.ServletConfig;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jspsmart.upload.SmartUpload;

public class TestUpLoad {
	private static int a = 0;

	public String TestUpLoad(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		if (a > 2) {
			throw new Exception("同时导入的用户太多,请稍候再导入!!!!");
		}
		a += 1;

		System.out.println("进入导入方法!!!!!");
		SmartUpload mySmartUpload = new SmartUpload();
		ServletConfig config = (ServletConfig) request
				.getAttribute("ServletConfig");
		try {

			// Initialization
			mySmartUpload.initialize(config, request, response);
			mySmartUpload.setTotalMaxFileSize(1000000 * 1024);
			mySmartUpload.setMaxFileSize(4000 * 1024);
			// Upload
			mySmartUpload.upload();
			// }
			com.jspsmart.upload.File myFile = mySmartUpload.getFiles().getFile(
					0);

			// 取得上载的文件的文件名
			String myFileNamesc = myFile.getFileName();
			// 取得不带后缀的文件名
			// String suffix = myFileName
			// .substring(0, myFileName.lastIndexOf('.'));
			// 取得后缀名
			String ext = mySmartUpload.getFiles().getFile(0).getFileExt();
			// 保存路径

			String path = config.getServletContext().getRealPath("/")
					+ "/upload/";
			System.err.println(path);
			// String trace=path+myFileName;
			String myFileName = System.currentTimeMillis() + "." + ext;
			String trace = path + "" + myFileName;
			System.err.println("路径是:    " + path);

			// Save the file with the original name
			// in a virtual path of the web server
			if (myFileNamesc != null && !myFileNamesc.equals("")) {
				myFile.saveAs(trace, SmartUpload.SAVE_PHYSICAL);
			}
			/**
			 * 删除原来的文件;
			 */

			if (this.deleteFile("")) {
				System.err.println("文件删除成功");
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("错误发生在" + e.getMessage());
		} finally {
			a -= 1;
		}
		return "";
	}

	public static boolean deleteFile(String url) {// url格式:/sca/
		boolean flag = false;
		File filed = new File(url);
		try {
			if (!filed.exists()) {
				flag = false;
			} else {
				filed.delete();
			}
			flag = true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return flag;
	}
}

⌨️ 快捷键说明

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