<form name="form" id="form" action="/zyk/zykpic.do" method="post" enctype="multipart/form-data" >
<input type="file" name="zykpicfile" id="zykpicfile" />支持格式:.jpg.jepg.bmp.png.gif
</form>
private FormFile zykpicfile;
public FormFile getZykpicfile() {
return zykpicfile;
}
public void setZykpicfile(FormFile zykpicfile) {
this.zykpicfile = zykpicfile;
}
public List picSet(FormFile picfile,File dir,String ymdhms,String file_configpath){
ArrayList l_picSet=new ArrayList();
FileOutputStream out = null;
try {
byte[] b = picfile.getFileData();// 取得文件数据
String fileName=ymdhms;//文件名称前缀
String extName = picfile.getFileName().substring(picfile.getFileName().lastIndexOf(".")); // 得到上传文件的扩展名
//.jpg.jepg.bmp.png.gif
if(extName.equals(".jpg") || extName.equals(".jepg") ||extName.equals(".bmp") ||extName.equals(".png") ||extName.equals(".gif")){
l_picSet.add("file");
}else{
l_picSet.add("errorfile");
}
l_picSet.add(picfile.getFileSize()/1024);//把图片大小转换为kb
String upname=fileName+extName;
l_picSet.add(upname);//把图片名称
String file_path=dir + File.separator + upname;
File outfile = new File(file_path);
out = new FileOutputStream(outfile, false);
out.write(b); // 通过流将数据写入文件
//处理图片大小
ImageSize imageSize=new ImageSize();
imageSize.zoomImg(file_path,file_configpath, 120, 80,"small"+upname);
}catch (Exception e) {
e.printStackTrace();
}finally {
if (out != null) {
try {
out.close(); // 关闭文件输出流
} catch (IOException e) {
e.printStackTrace();
}
}
}
return l_picSet;
}
//D:\tomcat6\webapps\zyk/pic/2011/06/15/20110615083615.jpg
String path_URL=sys_path+pic_configpath+datepath+"/"+filename;
File f = new File(path_URL);
if(f.exists()){
//需要判断是否存在
}
//-------------------------------
try {
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
OutputStream fos = null;
InputStream fis = null;
//如果是从服务器上取就用这个获得系统的绝对路径方法。 String filepath = servlet.getServletContext().getRealPath("/" + path);
String filepath=path_URL;
File uploadFile = new File(filepath);
fis = new FileInputStream(uploadFile);
bis = new BufferedInputStream(fis);
fos = response.getOutputStream();
bos = new BufferedOutputStream(fos);
//这个就就是弹出下载对话框的关键代码
response.setHeader("Content-disposition","attachment;filename=" +filename);
int bytesRead = 0;
//用输入流进行先读,然后用输出流去写,唯一不同的是我用的是缓冲输入输出流
byte[] buffer = new byte[65535];
while ((bytesRead = bis.read(buffer, 0, 65535)) != -1) {
bos.write(buffer, 0, bytesRead);
}
bos.flush();
fis.close();
bis.close();
fos.close();
bos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//访问那个action即可
<a href="/zyk/zykpic.do?method=downfile&picid=001">下载</a>