/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.li.action;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
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.actions.DispatchAction;
import org.apache.struts.upload.FormFile;
import com.li.form.UploadForm;
public class UploadAction extends DispatchAction
{
@SuppressWarnings("unchecked")
ArrayList list = new ArrayList();
public ActionForward fileDown(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws Exception
{
/*InputStream bis=null;
OutputStream bos=null;
String fileName = new String(request.getParameter("filename").getBytes(
"ISO-8859-1"), "GB2312");
String path="D:\\files\\"+fileName;
response.reset();
response.setContentType("application/x-msdownload");
response.addHeader("Content-Disposition", "attachment;filename=\""
+ new String(fileName.getBytes("GB2312"), "ISO-8859-1") + "\"");
try {
bis = new BufferedInputStream(new FileInputStream(path));
bos = new BufferedOutputStream(response.getOutputStream());
byte[] buff = new byte[20480];
while ((bis.read(buff, 0, buff.length)) != -1) {
bos.write(buff, 0, buff.length);
}
bos.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
bos.close();
bis.close();
} */
String fileName = new String(request.getParameter("filename").getBytes("ISO-8859-1"), "GB2312");
System.out.println(fileName);
String path="D:\\files\\"+fileName;
try {
// path是指欲下载的文件的路径。
File file = new File(path);
// 以流的形式下载文件。
InputStream fis = new BufferedInputStream(new FileInputStream(path));
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
// 清空response
response.reset();
// 设置response的Header
response.setContentType("application/x-msdownload");
response.addHeader("Content-Disposition", "attachment;filename=\""
+ new String(fileName.getBytes("uft-8"), "ISO-8859-1") + "\"");
OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
toClient.write(buffer);
toClient.flush();
toClient.close();
}
catch (IOException ex)
{
ex.printStackTrace();
}
return null;
}
@SuppressWarnings("static-access")
public ActionForward fileUpLoad(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws Exception
{
UploadForm uploadForm = (UploadForm) form;
FormFile fFile = uploadForm.getMyFile();
String fileType=fFile.getContentType();
int fileSize=fFile.getFileSize();
System.out.println("文件类型"+fileType);
System.out.println("文件大小"+fileSize);
FileOutputStream fos=null;
try
{
byte[] data=fFile.getFileData();
String fileName=fFile.getFileName();
int a=fileName.lastIndexOf(".");
String st = fileName.substring(a);
String newFileName = System.currentTimeMillis() + st; //修改上传文件的名称
System.out.println("文件名称"+newFileName);
/*ServletContext application =this.getServlet().getServletContext(); //保存到tomcat目录下
String realPath = application.getRealPath("/FILES/");*/
this.isNewFile(); //判断文件夹是否存在
String realPath = "D:\\files\\";
fos = new FileOutputStream(realPath + newFileName);
fos.write(data);
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
try
{
fos.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
list = showAllFiles(); //查找文件夹下的文件
request.setAttribute("list",list);
return mapping.findForward("SHOW");
}
@SuppressWarnings({ "unchecked", "static-access" })
public ActionForward moreFileUpLoad(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws Exception
{
UploadForm uploadForm = (UploadForm) form;
ArrayList myFlies = uploadForm.getMyFiles();
FileOutputStream fos=null;
this.isNewFile(); //判断文件夹是否存在
for(int i=0;i<myFlies.size();i++)
{
try
{
//System.out.println(phones.get(i));
FormFile ff = (FormFile)myFlies.get(i);
byte[] data=ff.getFileData();
String fileName=ff.getFileName();
/*int a=fileName.lastIndexOf(".");
String st = fileName.substring(a);
String newFileName = System.currentTimeMillis() + st;*/
System.out.println("文件名称"+fileName);
/*ServletContext application =this.getServlet().getServletContext();
String realPath = application.getRealPath("/FILES/");*/
String realPath = "D:\\files\\";
//fos = new FileOutputStream(realPath + "/" + fileName);
fos = new FileOutputStream(realPath + fileName);
fos.write(data);
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
try
{
fos.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
list = showAllFiles();
request.setAttribute("list",list);
return mapping.findForward("SHOW");
}
public static void isNewFile() throws IOException
{
File f = new File("D:\\files\\");
boolean b = f.exists();
if (b == false)
{
f.mkdir();
}
else
{
System.out.println("文件夹已存在");
}
}
@SuppressWarnings("unchecked")
public static ArrayList showAllFiles() throws Exception
{
File root = new File("D:\\files\\");
ArrayList aList = new ArrayList();
File[] fs = root.listFiles();
for(int i=0; i<fs.length; i++)
{
aList.add(fs[i].getName());
}
/*for(int j=0;j<aList.size();j++)
{
System.out.println(aList.get(j));
}*/
return aList;
}
public static void main(String[] args) throws Exception
{
System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
//showAllFiles();
}
}