文件上传下载(oreilly之MultipartReques)

上传篇代码片段:

import com.oreilly.servlet.MultipartRequest;
import java.util.Enumeration;

MultipartRequest multi = null;

ServletConfig config = super.getServletConfig();
int maxPostSize = 200 * 1024 * 1024;
multi = new MultipartRequest(request, filePath, maxPostSize, "utf-8");


fileName = multi.getParameter("fileName");
id = multi.getParameter("id");

Enumeration upLoadFiles = multi.getFileNames();

......
while (upLoadFiles.hasMoreElements()) {
		String fName = (String) upLoadFiles.nextElement();
		oldName = multi.getFilesystemName(fName);
	}

....
String exg = oldName.substring(oldName.lastIndexOf("."));
// System.out.println("--------" + exg);
String fileSep = System.getProperty("file.separator");
File oldfile = new File(filePath + fileSep + oldName)

 

下载篇:

if (null != action && action.equals("downFile")) {
			String filename = request.getParameter("fileName");
			// SmartUpload su = new SmartUpload();
			// su.initialize(config, request, response);
			// su.setContentDisposition(null);
			String downPath = FileUtil.getFileLocation(config, "");
			// // 下载文件
			// try {
			// su.downloadFile(downPath);
			// } catch (SmartUploadException e) {
			// // TODO Auto-generated catch block
			// e.printStackTrace();
			// }
			String encoding = System.getProperty("file.encoding");
			System.out.println("--------文件编码格式:-----------------" + encoding);
			String guessCharset = "utf-8";
			System.out.println();
			try { // 使用iso8559_1编码方式
				String isofilename = new String(
						filename.getBytes(guessCharset), "iso-8859-1");
				response.setContentType("application/octet-stream");
				response.setHeader("Content-Disposition",
						"attachment; filename=" + isofilename);
				ServletOutputStream out = null;
				out = response.getOutputStream();
				ServletUtils.returnFile(downPath + filename, out);// 下载文件
			} catch (UnsupportedEncodingException ex) {// iso8559_1编码异常
				ex.printStackTrace();
			} catch (IOException e) {// getOutputStream()异常。
				e.printStackTrace();
			}

		}

 

 

你可能感兴趣的:(servlet)