自定义路径文件上传

这篇文章是接上篇文章的

没看过上篇文章的请去看看

我在这边就直接贴出action代码了


/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.hcwy.struts.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.UUID;

import javax.servlet.ServletOutputStream;
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;

import com.hcwy.struts.form.UploadForm;

/**
* MyEclipse Struts
* Creation date: 04-08-2008
*
* XDoclet definition:
* @struts.action path="/upload" name="uploadForm" input="/form/upload.jsp" scope="request" validate="true"
*/
public class UploadAction extends Action {
/*
* Generated Methods
*/

/**
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
UploadForm uploadForm = (UploadForm) form;
String dir="C:\\upload\\"+UUID.randomUUID()+"."+(uploadForm.getPic().getFileName().split("\\.")[1]) ;
OutputStream out;
try {
out = new FileOutputStream(new File(dir));
byte b[]=uploadForm.getPic().getFileData();
out.write(b);
out.close();
File file=new File(dir);
ServletOutputStream os=response.getOutputStream();
FileInputStream fis= new FileInputStream(file);
byte buffer[] = new byte[8192];
int readed = 0;
while((readed = fis.read(buffer)) > 0) {
os.write(buffer, 0, readed);
}
if(os!=null) {
os.flush();
os.close();
}
if(fis!=null) {
fis.close();
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}








// public ActionForward asd(ActionMapping mapping, ActionForm form,
// HttpServletRequest request, HttpServletResponse response) {
// UploadForm uploadForm = (UploadForm) form;
//// System.out.println(request.getRequestURI());
//
//// System.out.println("** "+uploadPath) ;
// System.out.println(request.getSession().getServletContext().getRealPath(""));
// try {
// if(uploadForm.getPic().getFileName()==null||uploadForm.getPic().getFileName().equals("")){
//// request.setAttribute("dir", "dir");
// }else{
// String uploadPath = this.getServlet().getServletContext().getRealPath("/");
// String dir="\\upload\\"+UUID.randomUUID()+"."+(uploadForm.getPic().getFileName().split("\\.")[1]) ;
// String dir1=uploadPath+dir;
// System.out.println(dir1);
// OutputStream out=new FileOutputStream(new File(dir1));
// byte b[]=uploadForm.getPic().getFileData();
// out.write(b);
// out.close();
// request.setAttribute("dir", dir);
//
// }
//
//
// } catch (FileNotFoundException e) {
// e.printStackTrace();
// } catch (IOException e) {
// e.printStackTrace();
// }
// return mapping.findForward("ok");
// }
//











}


你可能感兴趣的:(JAVA文章)