我有一个上床下载的代码!
上床:
你的类{
if(!emailForm.getFile().getFileName().equals("")){
String savePath = "/Jsp_File/ll/Email/upload/";
//读取文件
FormFile file = emailForm.getFile();
//设置保存路径
String lastName = file.getFileName().substring(file.getFileName().length()-4);
String upLoadName = getThisDate() + lastName;
emailForm.setFILE_PATH(savePath + upLoadName);
//emailForm.setFILE_PATH(savePath + file.getFileName());
try {
InputStream stream = file.getInputStream();//把文件读入
String filePath = servlet.getServletContext().getRealPath(savePath);//取当前系统路径
OutputStream bos = new FileOutputStream(filePath +"/"+
upLoadName);//建立一个上传文件的输出流
System.out.println(filePath+"/"+upLoadName);
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ( (bytesRead = stream.read(buffer, 0, 8192)) != -1) {
bos.write(buffer, 0, bytesRead);//将文件写入服务器
}
bos.close();
stream.close();
}catch(Exception e){
System.err.print(e);
}
}else{
emailForm.setFILE_PATH("");
}
}
/*下载*/
public ActionForward download(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
//String fileName = request.getParameter("filePath");
System.out.println("select file_path from t_email where email_id = " + request.getParameter("emailid"));
Map filePathMap = Conn.executeQueryById("select file_path from t_email where email_id = " + request.getParameter("emailid"));
System.out.println(filePathMap.get("file_path").toString());
String fileName = filePathMap.get("file_path").toString();
System.out.println(fileName);
fileName = fileName==null?"":fileName;
fileName = fileName.trim();
InputStream inStream= null;
String attch_name = "";
byte[] b = new byte[100];
int len= 0;
try {
//取得附件的名称
attch_name = getAttachName(fileName);
fileName = getRealName(request,fileName);
if(fileName==null) {
request.setAttribute("msg","文件不存在");
request.getRequestDispatcher(request.getContextPath()+"/Jsp_File/cch/error/error.jsp").forward(request,response);
return null;
}
attch_name = toUtf8String(attch_name);
//读到流中
inStream=new FileInputStream(fileName);
//设置输出的格式
response.reset();
response.setContentType("application/x-msdownload");
response.addHeader("Content-Disposition","attachment; filename=\"" + attch_name + "\"");
//循环取出流中的数据
while((len=inStream.read(b)) >0) {
response.getOutputStream().write(b,0,len);
}
inStream.close();
}catch ( Exception e ){
if ( e instanceof java.io.FileNotFoundException ) {
try {
request.setAttribute("msg","下载文件出错");
request.getRequestDispatcher(request.getContextPath()+"/Jsp_File/cch/error/error.jsp").forward(request,response);
}catch (ServletException e1) {
e1.printStackTrace();
}catch ( IOException ex ) {
ex.printStackTrace(System.err);
}
}else {
e.printStackTrace(System.err);
}
}
return null;
}
/*下载*/
public static String getAttachName(String file_name) {
if(file_name==null) return "";
file_name = file_name.trim();
int iPos = 0;
iPos = file_name.lastIndexOf("\\");
if(iPos>-1){
file_name = file_name.substring(iPos+1);
}
iPos = file_name.lastIndexOf("/");
if(iPos>-1){
file_name = file_name.substring(iPos+1);
}
iPos = file_name.lastIndexOf(File.separator);
if(iPos>-1){
file_name = file_name.substring(iPos+1);
}
return file_name;
}
/*下载*/
public static String toUtf8String(String s) {
StringBuffer sb = new StringBuffer();
for (int i=0;i<s.length();i++) {
char c = s.charAt(i);
if (c >= 0 && c <= 255) {
sb.append(c);
} else {
byte[] b;
try {
b = Character.toString(c).getBytes("utf-8");
} catch (Exception ex) {
System.out.println(ex);
b = new byte[0];
}
for (int j = 0; j < b.length; j++) {
int k = b[j];
if (k < 0) k += 256;
sb.append("%" + Integer.toHexString(k).toUpperCase());
}
}
}
String s_utf8 = sb.toString();
sb.delete(0,sb.length());
sb.setLength(0);
sb = null;
return s_utf8;
}