?? upload.txt
字號(hào):
package com.hywavesoft.struts.upload;
import org.apache.struts.action.ActionForm;
import org.apache.struts.upload.FormFile;
public class UploadForm extends ActionForm {
private FormFile theFile;
public FormFile getTheFile() {
return theFile;
}
public void setTheFile(FormFile theFile) {
this.theFile = theFile;
}
}
package com.hywavesoft.struts.upload;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
public abstract class BaseAction extends Action {
protected static final String SAVE = "save";
protected static final String DELETE = "delete";
protected static final String DOWN = "DOWN";
protected static final String DATABASE_DEST = "database";
protected static final String FILE_DEST = "file";
public abstract ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception;
public String getPath(String filePath){
String path = getServlet().getServletContext().getRealPath(filePath) + "\\";
return path;
}
}
/**
* 這是一個(gè)輔助類,輔助完成上傳功能。
* 可以選擇將文件保存在數(shù)據(jù)庫(kù)里或保存在文件系統(tǒng)上
* 并對(duì)文件的類型和大小進(jìn)行了限制
*/
package com.hywavesoft.struts.commons;
import java.io.*;
public class UploadUtil {
private static final String DATABASE_DEST = "database";
private static final String FILE_DEST = "file";
private static final int MAX_SIZE = 1024 * 1024;
private static final String[] TYPES = {
".jpg", ".gif", ".zip", ".rar", ".doc"};
public static void saveFile(String fileName, byte[] fileData, int size,
String dest) throws FileNotFoundException,
IOException {
/* if (!checkSize(size)) {
throw new IOException(size + " is too large !");
}*/
if (!checkType(fileName)) {
throw new IOException("Unvaildate type !");
}
if (dest.equals(DATABASE_DEST)) {
saveToDb(fileName, fileData);
}
if (dest.equals(FILE_DEST)) {
saveToFile(fileName, fileData);
}
}
private static void saveToDb(String fileName, byte[] fileData) {
}
private static void saveToFile(String fileName, byte[] fileData)
throws FileNotFoundException, IOException {
OutputStream o = new FileOutputStream(fileName);
o.write(fileData);
o.close();
}
public static void delFile(String fileName, String dest)
throws NullPointerException, SecurityException {
if (dest.equals(DATABASE_DEST)) {
delFromDb(fileName);
}
if (dest.equals(FILE_DEST)) {
delFromFile(fileName);
}
}
private static void delFromDb(String fileName) {
}
private static void delFromFile(String fileName)
throws NullPointerException, SecurityException {
File file = new File(fileName);
if (file.exists()) {
file.delete();
}
}
private static boolean checkSize(int size) {
if (size > MAX_SIZE) {
return false;
}
return true;
}
private static boolean checkType(String fileName) {
for (int i = 0; i < TYPES.length; i++) {
if (fileName.toLowerCase().endsWith(TYPES[i])) {
return true;
}
}
return false;
}
public static String getContentType(String fileName) {
String fileNameTmp = fileName.toLowerCase();
String ret = "";
if (fileNameTmp.endsWith("txt")) {
ret = "text/plain";
}
if (fileNameTmp.endsWith("gif")) {
ret = "image/gif";
}
if (fileNameTmp.endsWith("jpg")) {
ret = "image/jpeg";
}
if (fileNameTmp.endsWith("jpeg")) {
ret = "image/jpeg";
}
if (fileNameTmp.endsWith("jpe")) {
ret = "image/jpeg";
}
if (fileNameTmp.endsWith("zip")) {
ret = "application/zip";
}
if (fileNameTmp.endsWith("rar")) {
ret = "application/rar";
}
if (fileNameTmp.endsWith("doc")) {
ret = "application/msword";
}
if (fileNameTmp.endsWith("ppt")) {
ret = "application/vnd.ms-powerpoint";
}
if (fileNameTmp.endsWith("xls")) {
ret = "application/vnd.ms-excel";
}
if (fileNameTmp.endsWith("html")) {
ret = "text/html";
}
if (fileNameTmp.endsWith("htm")) {
ret = "text/html";
}
if (fileNameTmp.endsWith("tif")) {
ret = "image/tiff";
}
if (fileNameTmp.endsWith("tiff")) {
ret = "image/tiff";
}
if (fileNameTmp.endsWith("pdf")) {
ret = "application/pdf";
}
return ret;
}
}
package com.hywavesoft.struts.upload;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;
import java.io.*;
import java.net.URLEncoder;
import com.hywavesoft.struts.commons.UploadUtil;
public class downAction
extends BaseAction {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception {
UploadForm uploadForm = (UploadForm) form;
String fileName = request.getParameter("filename");
String sysroot = servlet.getServletContext().getInitParameter("sysroot");
File file = new File(getPath(sysroot) + fileName);//
if(file.exists()){
try{
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
byte[] buffer = new byte[1024];
fileName = java.net.URLEncoder.encode(fileName, "UTF-8");//處理中文文件名的問題
fileName = new String(fileName.getBytes("UTF-8"),"GBK");//處理中文文件名的問題
response.reset();
// response.setCharacterEncoding("UTF-8");
// response.setContentType("application/x-rar-compressed");//不同類型的文件對(duì)應(yīng)不同的MIME類型
response.setContentType(UploadUtil.getContentType(fileName));
response.setHeader("Content-Disposition","attachment; filename=" + fileName);
OutputStream os = response.getOutputStream();
while(bis.read(buffer) > 0){
os.write(buffer);
}
bis.close();
os.close();
}
catch (IOException e) {
System.err.print(e);
}
}
/* BufferedInputStream bis = null;
BufferedOutputStream bos = null;
OutputStream fos = null;
InputStream fis = null;
try {
response.setContentType(UploadUtil.getContentType(fileName));
response.setHeader("Content-disposition", "attachment;filename="
+ URLEncoder.encode(fileName, "utf-8"));
// response.setHeader("Content-Disposition","attachment; filename=" + fileName);
fis = new FileInputStream(getPath(sysroot) + fileName);
bis = new BufferedInputStream(fis);
fos = response.getOutputStream();
bos = new BufferedOutputStream(fos);
int bytesRead = 0;
byte[] buffer = new byte[5 * 1024];
while ( (bytesRead = bis.read(buffer)) != -1) {
bos.write(buffer, 0, bytesRead); //將文件發(fā)送到客戶端
}
}
catch (IOException e) {
// response.setContentType("text/html");
response.reset();
//設(shè)置文件物理下載時(shí)出現(xiàn)的錯(cuò)誤信息
// this.setSysMessage(request, "download.failed", "btn.reupload","FileUpload.do?act=showFile");
return mapping.findForward("error");
}
finally {
try {
if (fos != null) {
fos.close();
}
if (bos != null) {
bos.close();
}
if (fis != null) {
fis.close();
}
if (bis != null) {
bis.close();
}
}
catch (IOException e) {
System.err.print(e);
}
}*/
return mapping.findForward("success");
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -