/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.techmore.struts.action;
import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.MappingDispatchAction;
/**
* MyEclipse Struts Creation date: 05-18-2007
*
* XDoclet definition:
*
* @struts.action validate="true"
*/
public class UploadFilesAction extends MappingDispatchAction {
/*
* Generated Methods
*/
/**
* Method execute
*
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
public ActionForward upload(ActionMapping mapping, ActionForm form, HttpServletRequest request,
HttpServletResponse response) {
// TODO Auto-generated method stub
System.out.println("xxx");
try {
request.setCharacterEncoding("UTF-8");
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
// upload.setHeaderEncoding(request.getCharacterEncoding());
System.out.println("request:" + request);
List items = upload.parseRequest(request);
Iterator iter = items.iterator();
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
if (item.isFormField()) {/*
* String name = item.getFieldName(); String value = item.getString();
* System.out.println("name = " + name); System.out.println("value = " +
* value); System.out.println("/r/n");
*/
} else {
String fieldName = item.getFieldName();
String fileName = item.getName();
System.out
.println("文件类型 " + fileName.substring(fileName.lastIndexOf('.') + 1, fileName.length()));
// boolean isInMemory = item.isInMemory();
long sizeInBytes = item.getSize();
getListInSession(request).add(fileName);
getListInSession(request).add(UUID.randomUUID());
// String[] str=fieldName.split("\\.");
// int l=str.length-1;
// System.out.println("文件类型 "+str[l]);
//
System.out.println("fieldName = " + fieldName);
System.out.println("fileName = " + fileName);
// System.out.println("文件存储路径 "+request.getRealPath("/")+"uploadfile/");
// 路径 D:\Program Files\apache-tomcat-6.0.18\webapps\rockontrol_oa\UploadFiles\2009042991039312\
String filePath = this.getServlet().getServletContext().getRealPath("/") + "uploadfile/" + "中文/";
File savePath = new File(filePath);
System.out.println(savePath.getAbsolutePath());
if (!savePath.exists()) {
savePath.mkdir();
}
item.write(new File(filePath + fileName));
}
}
System.out.println("-------- final--------");
} catch (Exception ex) {
ex.printStackTrace();
}
return null;
}
public ActionForward getResult(ActionMapping mapping, ActionForm form, HttpServletRequest request,
HttpServletResponse response) throws Exception {
List list = getListInSession(request);
request.getSession().removeAttribute("_result");
response.setCharacterEncoding("utf-8");
response.getWriter().print(list);
response.flushBuffer();
return null;
}
public List getListInSession(HttpServletRequest request) {
List result = (List) request.getSession().getAttribute("_result");
if (result == null) {
result = new ArrayList();
request.getSession().setAttribute("_result", result);
}
return result;
}
}
- 1
- 2
前往页