用jspsmartupload,google搜一下
解决方案 »
- struts的result中path和name对应,通过name跳到path指向的路径是用了什么设计模式?
- html里按回车键实现点击按钮的效果(java)
- 请问有人在配置eclipse和tomcat的时候出现过这种问题吗……
- java高手~求解~~~,注册根据注册名,生成二级域名,或者转向代码
- 放100分,想认识一些北京的朋友!
- 创建文件目录时mkdirs();和mkdir();这两个有何区别?
- 本来想明天不去上班了,工资是拿到了。但是老板不让走,说是让明天过去好好谈谈。在线等,明天快到了,着急!
- 为什么这一段会报错?jsp中的javascript
- 关于lucene中多条件搜索文件的问题,各位帮忙啊
- 紧急求助(我为什么调用不了bean)
- 我在一个jsp页面点下载,文件下载后,jsp页面中的js代码报错:访问拒绝。但刷新jsp页面后又没事了,这是什么问题啊?
- 文件下载出了问题,搞了半天了!
解决方案:1。 直接应运 jspSmartUpload 中提供的方法。
方法:
// Initialization
mySmartUpload.initialize(pageContext);
// Download file 如果你是 linux 服务器那么你可以这样做:
mySmartUpload.downloadFile("/服务器软件配置的路径(可以不填写)/相对路径/sample.zip");
// With a physical path 如果是 window 的服务器
// mySmartUpload.downloadFile("c:\\temp\\sample.zip")
// With options
// mySmartUpload.downloadFile("/upload/sample.zip","application/x-zip-compressed","downloaded.zip")
2。 如果是 linux 服务器 那么直接的到文件的路径,和文件名。作为一个链接点击可以直接下载。
值得注意的是 :
如果文件为 .txt 文件那么将在 浏览器里面直接显示。
如果是 图片如: gif 那么一样在浏览器里面直接打开。
如果为其他文件则直接下载。 解决 办法: 下载时将他们作为 .zip 文件直接下载。 public void downloadFile(java.lang.String sourceFilePathName,
java.lang.String contentType,
java.lang.String destFileName)
先说上穿
upload.jsp<%@ page contentType="text/html; charset=gb2312" language="java" import="java.util.*" errorPage="" %>
<html>
<head>
<title>文件上载</title>
</head>
<body>
<center>
<form name="uploadfile" method="post" onsubmit="" enctype="multipart/form-data" action="UpLoadMedia.jsp">
<table width="100%" border="0">
<tr>
<td width="29%" align="right" valign="middle"><span class="style2">请选择需要上传的文件:</span></td>
<td width="30%"><input name="filePath" type="file" class=adminbutton id="filePath" size="30"></td>
<td width="41%"><input name="upload" type="submit" class=adminbutton value="上载"></td>
</tr>
</table>
</form>
</center>
</body>
</html>----------------------------
UpLoadMedia.jsp<%@ page contentType="text/html; charset=gb2312" language="java" import="java.util.*" errorPage="" %>
<jsp:useBean id="upload1" scope="page" class="Memory.UpLoad" >
<jsp:setProperty name="upload1" property="uploadDirectory" value="/home/bigs.com.cn/web/www/temp/" />
</jsp:useBean>
<%
String filename = upload1.uploadFile(request);
%><html<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<body>
<center>
<p><font color=red>文件<%=toChinese(filename)%>成功上载!</font>
</p>
<p><a href="msend.jsp">返回</a></p>
</center>
</body>
</html>----------------------------------
UpLoad Bean的代码 UpLoad.javapackage Memory;import java.io.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.ServletInputStream;
import javax.servlet.ServletException;public class UpLoad {
private static String newline = "\n";
private String uploadDirectory = ".";
private String ContentType = "";
private String CharacterEncoding = "";
String filename = null;
public UpLoad() {
} private String getFileName(String s) {
int i = s.lastIndexOf("\\");
if(i < 0 || i >= s.length() - 1) {
i = s.lastIndexOf("/");
if(i < 0 || i >= s.length() - 1)
return s;
}
return s.substring(i + 1);
} public void setUploadDirectory(String s) {
uploadDirectory = s;
} public void setContentType(String s) {
ContentType = s;
int j;
if((j = ContentType.indexOf("boundary=")) != -1) {
ContentType = ContentType.substring(j + 9);
ContentType = "--" + ContentType;
}
} public void setCharacterEncoding(String s) {
CharacterEncoding = s;
} public String uploadFile( HttpServletRequest req) throws ServletException, IOException {
setCharacterEncoding(req.getCharacterEncoding());
setContentType(req.getContentType());
uploadFile(req.getInputStream());
return filename;
} public void uploadFile( ServletInputStream servletinputstream) throws
ServletException, IOException { String s5 = null;
byte Linebyte[] = new byte[4096];
byte outLinebyte[] = new byte[4096];
int ai[] = new int[1];
int ai1[] = new int[1]; String line;
while((line = readLine(Linebyte, ai, servletinputstream, CharacterEncoding)) != null) {
int i = line.indexOf("filename=");
if(i >= 0) {
line = line.substring(i + 10);
if((i = line.indexOf("\"")) > 0)
line = line.substring(0, i);
break;
}
} filename = line; if(filename != null && !filename.equals("\"")) {
filename = getFileName(filename); String sContentType = readLine(Linebyte, ai, servletinputstream, CharacterEncoding);
if(sContentType.indexOf("Content-Type") >= 0)
readLine(Linebyte, ai, servletinputstream, CharacterEncoding); File file = new File(uploadDirectory, filename); FileOutputStream fileoutputstream = new FileOutputStream(file); while((sContentType = readLine(Linebyte, ai, servletinputstream, CharacterEncoding)) != null) {
if(sContentType.indexOf(ContentType) == 0 && Linebyte[0] == 45)
break; if(s5 != null) {
fileoutputstream.write(outLinebyte, 0, ai1[0]);
fileoutputstream.flush();
}
s5 = readLine(outLinebyte, ai1, servletinputstream, CharacterEncoding);
if(s5 == null || s5.indexOf(ContentType) == 0 && outLinebyte[0] == 45)
break;
fileoutputstream.write(Linebyte, 0, ai[0]);
fileoutputstream.flush();
} byte byte0;
if(newline.length() == 1)
byte0 = 2;
else
byte0 = 1;
if(s5 != null && outLinebyte[0] != 45 && ai1[0] > newline.length() * byte0)
fileoutputstream.write(outLinebyte, 0, ai1[0] - newline.length() * byte0);
if(sContentType != null && Linebyte[0] != 45 && ai[0] > newline.length() * byte0)
fileoutputstream.write(Linebyte, 0, ai[0] - newline.length() * byte0);
fileoutputstream.close();
}
} private String readLine(byte Linebyte[], int ai[], ServletInputStream servletinputstream,
String CharacterEncoding) { try{
ai[0] = servletinputstream.readLine(Linebyte, 0, Linebyte.length);
if(ai[0] == -1)
return null;
} catch(IOException _ex) {
return null;
}
try {
if(CharacterEncoding == null) {
return new String(Linebyte, 0, ai[0]);
} else {
return new String(Linebyte, 0, ai[0], CharacterEncoding);
}
} catch(Exception _ex) {
return null;
}
}
}
--------------------------------------
上传功能实现,请给50分
下载其实更加简单请看下面的代码:
download.jsp<%@ page contentType="text/html; charset=gbk"%>
<%@ page language="java" import="java.util.*"%>
<%@ page import="java.lang.*"%>
<%@ page import="javax.mail.*"%>
<%@ page import="javax.mail.internet.*"%>
<%@ page import="java.io.*"%>
<%@ page import="java.text.SimpleDateFormat"%><%
try
{
//BufferedInputStream is = new BufferedInputStream(part.getInputStream());FileInputStream is = new FileInputStream("文件的完整路径"); response.setContentType("application/octet-stream");
response.setHeader ("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
response.setHeader("Accept-ranges", "bytes"); ServletOutputStream output = response.getOutputStream(); byte[] buf=null; int l=0; while ((buf[i] = is.read()) != -1)
{
output.write(buf[i]);
i++;
}
output.write(buf); is.close();
output.close();}catch(javax.mail.MessagingException e){
System.out.println(e.getMessage());
}
%>原理就是把服务器文件打开成一个InputStream,然后通过
response.setContentType("application/octet-stream");
response.setHeader ("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
response.setHeader("Accept-ranges", "bytes");
这样的操作给向浏览器输出的流+一个header 让浏览知道我是一个比特流,名字是什么,实现了下载。
文件名:download.html
作 者:纵横软件制作中心雨亦奇([email protected])
-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>下载</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body>
<a href="jsp/do_download.jsp">点击下载</a>
</body>
</html>
2、下载处理页面do_download.jsp do_download.jsp展示了如何利用jspSmartUpload组件来下载文件,从下面的源码中就可以看到,下载何其简单。 源码如下: <%@ page contentType="text/html;charset=gb2312"
import="com.jspsmart.upload.*" %><%
// 新建一个SmartUpload对象
SmartUpload su = new SmartUpload();
// 初始化
su.initialize(pageContext);
// 设定contentDisposition为null以禁止浏览器自动打开文件,
//保证点击链接后是下载文件。若不设定,则下载的文件扩展名为
//doc时,浏览器将自动用word打开它。扩展名为pdf时,
//浏览器将用acrobat打开。
su.setContentDisposition(null);
// 下载文件
su.downloadFile("/upload/如何赚取我的第一桶金.doc");
%>
TO(蓝鲸):非常感激你,不过我却没有办法根据你的代码来实现,当然小弟是新手,所以后来就选择了smartupload,还请指正。