1.文件下载
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.URLEncoder;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Download extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String filepath = this.getServletContext().getRealPath("download//呵呵.jpg");
System.out.println(filepath);
int index = filepath.indexOf("\\");
String fileName = filepath.substring(index+1);
response.setHeader("content-disposition","attachment;fileName="+URLEncoder.encode(fileName,"utf-8"));
InputStream in = new FileInputStream(filepath);
OutputStream out = response.getOutputStream();
byte[] b = new byte[1024];
int len = 0;
while((len = in.read(b))!=-1){
out.write(b, 0, len);
}
in.close();
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
}
2 文件上传 运用 smartupload 组件
import java.io.File;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.jspsmart.upload.Files;
import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;
public class RefactorUpload extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
IpTimeNum iPTimeNum = new IpTimeNum(request.getRemoteAddr());
SmartUpload su = new SmartUpload();
su.initialize(this.getServletConfig(),request,response);
try {
String path = this.getServletContext().getRealPath("/")+"/file";
//
su.upload();
Files file = su.getFiles(); // 获取文件组
com.jspsmart.upload.File files=file.getFile(0);
System.out.println(files.getFileExt());
String fname =files.getFileName();
//实例化文件对象
String Filename =iPTimeNum.getIPTimeRand()+fname;
su.getFiles().getFile(0).saveAs(java.io.File.separator+"/file"+java.io.File.separator+Filename);
String fileName = iPTimeNum.getIPTimeRand();
response.getWriter().print("成功上传文件:"+fileName);
} catch (SmartUploadException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
2.1 创建 upload.jsp
<form action="RefactorUpload" method="post" enctype="multipart/form-data">
<input type="text" name="name"/>
<input type="file" name="file"/>
<input type="submit" value="上传">
</form>
</body>
3 文件下载 运用 smartupload组件
-
Index.jsp
<div align="center">
<A HREF="download1.jsp?path=<%=basePath+"file/011.jpg"%>">
<img src="file/download.GIF" width="22"height="22"border="0"></A>
</div>
2.download.jsp
<%
response.setCharacterEncoding("utf-8");
String path=request.getParameter("path");
//path=new String(path.getBytes("iso-8859-1"));
String fileName = path.substring(path.lastIndexOf("file"));
SmartUpload su = new SmartUpload(); // 新建一个smartupload对象
su.initialize(pageContext); // 初始化准备操作
su.setContentDisposition(null);
su.downloadFile(fileName);
//解决异常
out.clear();
out = pageContext.pushBody();
%>