如何实现文件上传并浏览(100分)

  • 主题发起人 主题发起人 saisi
  • 开始时间 开始时间
有没有人用com.oreilly.servlet浏览文件
 
去下实例吧,这么多,说不清楚3!
 
哪里有的下?
 
用jspsmartup如何
 
有例子吗?
 
http://tech.163.com/tm/001206/001206_12031.html
http://www.chinabs.net/java/default.asp?infoid=30
 
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class UploadServlet extends HttpServlet
{
private static final String REPLY = "reply";
private static final String DIR = "dir";
private static final String VERSION = "ver. 1.33";
private static final String NAME = "name";
private static final String PORT = "port";
private static final String FROM = "from";
private static final String TO = "to";
private static final Stringdo
MAIN = "domain";
private static final String HOST = "host";
private static final String SUBJECT = "subject";
private static final String LOG = "log";
private static final String EDITED = "edited";
private static final String DEFAULT_DIR = ".";
private static final String DEFAULT_PORT = "25";
private static final String DEFAULT_SUBJECT = "see attachment for the uploaded file";
private static final String CPR = "&amp;nbsp;&amp;copy;&amp;nbsp;<a href=/"mailto:coldjava@usa.net/">Coldjava</a>&amp;nbsp;ver. 1.3" +
"3"
;
static String separator = "";
static String newline = "/n";
private static Hashtable cfgs;
private static Object forLock = new Object();
private static Object forLock1 = new Object();
private static Object SessionIdLock = new Object();
private static int HOW_LONG = 6;
public void init(ServletConfig servletconfig)
throws ServletException
{
super.init(servletconfig);
separator = System.getProperty("file.separator");
newline = System.getProperty("line.separator");
cfgs = new Hashtable();
}
public voiddo
Get(HttpServletRequest httpservletrequest, HttpServletResponse httpservletresponse)
throws ServletException, IOException
{
do
Post(httpservletrequest, httpservletresponse);
}
public voiddo
Post(HttpServletRequest httpservletrequest, HttpServletResponse httpservletresponse)
throws ServletException, IOException
{
String s = "";
String s3 = "";
String s4 = "";
String s5 = null;
String s6 = null;
ServletInputStream servletinputstream = null;
String s1 = httpservletrequest.getQueryString();
Hashtable hashtable = getConfig(s1);
s3 = (String)hashtable.get("dir");
s4 = (String)hashtable.get("reply");
s = (String)hashtable.get("name");
servletinputstream = httpservletrequest.getInputStream();
s5 = httpservletrequest.getCharacterEncoding();
s6 = httpservletrequest.getContentType();
int i;
if((i = s6.indexOf("boundary=")) != -1)
{
s6 = s6.substring(i + 9);
s6 = "--" + s6;
}
try
{
s1 = UploadFile(s, s3, s6, servletinputstream, s5);
}
catch(Exception _ex)
{
httpservletresponse.setContentType("text/html");
PrintWriter printwriter = httpservletresponse.getWriter();
printwriter.println("<html>");
printwriter.println("<br><br>Could not upload file");
printwriter.println("<br><br><br>&amp;nbsp;&amp;copy;&amp;nbsp;<a href=/"mailto:coldjava@usa.net/">Coldjava</a>&amp;n" +
"bsp;ver. 1.33"
);
printwriter.println("</html>");
printwriter.flush();
printwriter.close();
return;
}
String s2;
if((s2 = (String)hashtable.get("log")) != null)
{
writeLog(s2, httpservletrequest.getRemoteAddr(), s1);
}
sendMail(s3, s1, hashtable);
if(s4 != null)
{
httpservletresponse.sendRedirect(s4);
return;
} else
{
httpservletresponse.setContentType("text/html");
PrintWriter printwriter1 = httpservletresponse.getWriter();
printwriter1.println("<html>");
printwriter1.println("<head>");
printwriter1.println("<title>Upload servlet</title>");
printwriter1.println("</head>");
printwriter1.println("<body>");
printwriter1.println("<br><br>File: " + s1 + " has been uploaded");
printwriter1.println("<br><br><br>&amp;nbsp;&amp;copy;&amp;nbsp;<a href=/"mailto:coldjava@usa.net/">Coldjava</a>&amp;n" +
"bsp;ver. 1.33"
);
printwriter1.println("</body></html>");
printwriter1.flush();
printwriter1.close();
return;
}
}
private void writeLog(String s, String s1, String s2)
{
synchronized(forLock)
{
try
{
PrintWriter printwriter = new PrintWriter(new FileWriter(s, true));
printwriter.println((new Date()).toString() + " " + (s1 != null ? s1 : "-") + " " + s2);
printwriter.close();
}
catch(Exception _ex) { }
}
}
private String UploadFile(String s, String s1, String s2, ServletInputStream servletinputstream, String s3)
throws ServletException, IOException
{
String s6 = null;
String s7 = "";
byte abyte0[] = new byte[4096];
byte abyte1[] = new byte[4096];
int ai[] = new int[1];
int ai1[] = new int[1];
long l = 0L;
String s8 = getId();
String s4;
while((s4 = readLine(abyte0, ai, servletinputstream, s3)) != null)
{
int i = s4.indexOf("filename=");
if(i >= 0)
{
s4 = s4.substring(i + 10);
i = s4.indexOf("/"");
if(i > 0)
{
s4 = s4.substring(0, i);
}
break;
}
}
if(s != null)
{
s4 = s;
}
s7 = s4;
if(s7 != null)
{
s7 = getFileName(s7);
String s5 = readLine(abyte0, ai, servletinputstream, s3);
if(s5.indexOf("Content-Type") >= 0)
{
readLine(abyte0, ai, servletinputstream, s3);
}
File file = new File(s1, s8);
FileOutputStream fileoutputstream = new FileOutputStream(file);
while((s5 = readLine(abyte0, ai, servletinputstream, s3)) != null)
{
l++;
if(s5.indexOf(s2) == 0 &amp;&amp;
abyte0[0] == 45)
{
break;
}
if(s6 != null &amp;&amp;
l <= 75L)
{
fileoutputstream.write(abyte1, 0, ai1[0]);
fileoutputstream.flush();
}
s6 = readLine(abyte1, ai1, servletinputstream, s3);
if(s6 == null || s6.indexOf(s2) == 0 &amp;&amp;
abyte1[0] == 45)
{
break;
}
fileoutputstream.write(abyte0, 0, ai[0]);
fileoutputstream.flush();
}
byte byte0;
if(newline.length() == 1)
{
byte0 = 2;
} else
{
byte0 = 1;
}
if(s6 != null &amp;&amp;
abyte1[0] != 45 &amp;&amp;
ai1[0] > newline.length() * byte0)
{
fileoutputstream.write(abyte1, 0, ai1[0] - newline.length() * byte0);
}
if(s5 != null &amp;&amp;
abyte0[0] != 45 &amp;&amp;
ai[0] > newline.length() * byte0)
{
fileoutputstream.write(abyte0, 0, ai[0] - newline.length() * byte0);
}
fileoutputstream.flush();
fileoutputstream.close();
try
{
synchronized(forLock1)
{
File file1 = new File(s1, s7);
try
{
file1.delete();
}
catch(Exception _ex) { }
file.renameTo(file1);
}
}
catch(Exception _ex) { }
} else
{
throw new IOException();
}
return s7;
}
private void sendMail(String s, String s1, Hashtable hashtable)
{
String s2 = (String)hashtable.get("port");
String s3 = (String)hashtable.get("domain");
String s4 = (String)hashtable.get("host");
String s5 = (String)hashtable.get("from");
String s6 = (String)hashtable.get("to");
if(s2 != null &amp;&amp;
s3 != null &amp;&amp;
s4 != null &amp;&amp;
s5 != null &amp;&amp;
s6 != null)
{
smtpMail smtpmail = new smtpMail();
int i;
try
{
i = Integer.parseInt(s2);
}
catch(Exception _ex)
{
return;
}
if(smtpmail.open(s4, i) != 1)
{
return;
}
smtpmail.setDomain(s3);
smtpmail.setFrom(s5);
smtpmail.setTo(s6);
String s7;
if((s7 = (String)hashtable.get("subject")) == null)
{
s7 = "";
}
smtpmail.addHeader("Subject", s7);
s7 = s;
if(!s7.endsWith(separator))
{
s7 = s7 + separator;
}
s7 = s7 + s1;
smtpmail.addAttachment(s7);
smtpmail.transmit();
smtpmail.close();
}
}
private String readLine(byte abyte0[], int ai[], ServletInputStream servletinputstream, String s)
{
try
{
ai[0] = servletinputstream.readLine(abyte0, 0, abyte0.length);
if(ai[0] == -1)
{
return null;
}
}
catch(IOException _ex)
{
return null;
}
try
{
if(s == null)
{
return new String(abyte0, 0, ai[0]);
} else
{
return new String(abyte0, 0, ai[0], s);
}
}
catch(Exception _ex)
{
return null;
}
}
private Hashtable getConfig(String s)
{
Hashtable hashtable;
if(s != null)
{
hashtable = (Hashtable)cfgs.get(s);
} else
{
hashtable = getDefaultConfig();
}
if(hashtable != null)
{
if(s != null)
{
String s1 = (String)hashtable.get("edited");
File file = new File(s);
if(file == null)
{
cfgs.remove(s);
} else
if(!s1.equals(String.valueOf(file.lastModified())))
{
cfgs.remove(s);
} else
{
return hashtable;
}
} else
{
return hashtable;
}
}
hashtable = new Hashtable();
readConfig(hashtable, s);
cfgs.put(s, hashtable);
return hashtable;
}
private Hashtable getDefaultConfig()
{
Hashtable hashtable = new Hashtable();
hashtable.put("dir", ".");
hashtable.put("edited", "");
return hashtable;
}
private void readConfig(Hashtable hashtable, String s)
{
try
{
BufferedReader bufferedreader = new BufferedReader(new InputStreamReader(new FileInputStream(s)));
String s1;
while((s1 = bufferedreader.readLine()) != null)
{
s1 = s1.trim();
if(s1.length() > 0)
{
int i = s1.indexOf("=");
if(i > 0 &amp;&amp;
i < s1.length() - 1 &amp;&amp;
s1.charAt(0) != '#' &amp;&amp;
!s1.startsWith("//"))
{
hashtable.put(s1.substring(0, i).trim(), s1.substring(i + 1).trim());
}
}
}
bufferedreader.close();
File file = new File(s);
hashtable.put("edited", String.valueOf(file.lastModified()));
}
catch(Exception _ex) { }
String s2;
if((s2 = (String)hashtable.get("port")) != null)
{
try
{
int j = Integer.parseInt(s2);
if(j <= 0)
{
hashtable.remove("port");
}
}
catch(Exception _ex)
{
hashtable.remove("port");
}
}
if((s2 = (String)hashtable.get("domain")) != null &amp;&amp;
s2.length() == 0)
{
hashtable.remove("domain");
}
if((s2 = (String)hashtable.get("reply")) != null &amp;&amp;
s2.length() == 0)
{
hashtable.remove("reply");
}
if((s2 = (String)hashtable.get("from")) != null &amp;&amp;
s2.length() == 0)
{
hashtable.remove("from");
}
if((s2 = (String)hashtable.get("to")) != null &amp;&amp;
s2.length() == 0)
{
hashtable.remove("to");
}
if((s2 = (String)hashtable.get("name")) != null &amp;&amp;
s2.length() == 0)
{
hashtable.remove("name");
}
if((s2 = (String)hashtable.get("dir")) != null &amp;&amp;
s2.length() == 0)
{
hashtable.remove("dir");
}
if(hashtable.get("port") == null)
{
hashtable.put("port", "25");
}
if(hashtable.get("subject") == null)
{
hashtable.put("subject", "see attachment for the uploaded file");
}
if(hashtable.get("dir") == null)
{
hashtable.put("dir", ".");
}
if(hashtable.get("edited") == null)
{
hashtable.put("edited", "");
}
}
private String getId()
{
String s = "";
synchronized(SessionIdLock)
{
long l = System.currentTimeMillis();
Random random = new Random();
s = String.valueOf(l);
for(int i = 1;
i <= HOW_LONG;
i++)
{
s = s + (int)(1.0D + (double)HOW_LONG * random.nextDouble());
}
}
return s;
}
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 String getServletInfo()
{
return "A UploadServlet";
}
public UploadServlet()
{
}
}
 
用oreilly那个包就可以了,我用它上传图片进数据库,很简单就搞定了
只是上传文件就更简单了
 
强烈关注ing,一定要灌水
 
lsj, 能不能详细说明一下怎样把上传的图片下载下来。
有相关网站能介绍几个么?
 
把显示图片做进一个servlet中,jsp中引用" <img src=yourservlet/>"
如果需要,我可以将这部分代码帖出来
 
我想知道主要是浏览文件精神
 
用jspsmartup实现上传和下载
不用存入数据库,只是在服务器管理好文件就可以,很简单
如果你要我给你类和例子,你的信箱是?
 
我用了jspsmartfile,但是会过期的
 
saisi
用反编译,可以得到原代码的
如果不行我可以给你
 
谢谢!kevin_zhou@sina.com
 
后退
顶部