急手的文件上传问题~~~~~~~~!
怕怕哥 2004-09-28 11:59:41 以下代码,不知道哪地方错了,请给予帮助.
// FrontEnd Plus GUI for JAD
// DeCompiled : servletUpload.class
package blue;
import com.jspsmart.upload.Request;
import com.jspsmart.upload.SmartUpload;
import java.io.*;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.*;
import javax.servlet.http.*;
public class uploadimages extends HttpServlet
{
private ServletConfig config;
public uploadimages()
{
}
public void destroy()
{
}
public void doGet(HttpServletRequest httpservletrequest, HttpServletResponse httpservletresponse)
throws ServletException, IOException
{
PrintWriter printwriter = httpservletresponse.getWriter();
httpservletresponse.setContentType("text/html;charset=gb2312");
printwriter.println("<HTML>");
printwriter.println("<BODY BGCOLOR='white'>");
printwriter.println("<H1>jspSmartUpload : Servlet Sample</H1>");
printwriter.println("<HR><BR>");
printwriter.println("The method of the HTML form must be POST.");
printwriter.println("</BODY>");
printwriter.println("</HTML>");
}
protected void doPost(HttpServletRequest httpservletrequest, HttpServletResponse httpservletresponse)
throws ServletException, IOException
{
PrintWriter printwriter = httpservletresponse.getWriter();
httpservletresponse.setContentType("text/html;charset=gb2312");
String imagesavename=new String();
boolean flag = false;
SmartUpload smartupload = new SmartUpload();
try
{
smartupload.initialize(config, httpservletrequest, httpservletresponse);
smartupload.upload();
java.util.Enumeration e = smartupload.getRequest().getParameterNames();
while (e.hasMoreElements()) {
String key = (String)e.nextElement();
String[] values = smartupload.getRequest().getParameterValues(key);
for(int i = 0; i < values.length; i++) {
if (key.equalsIgnoreCase("uname")) imagesavename=values[i];
}
}
for (int i=0;i<smartupload.getFiles().getCount();i++){
com.jspsmart.upload.File myFile = smartupload.getFiles().getFile(i);
myFile.saveAs(imagesavename);
if (!smartupload.getFiles().getFile(i).isMissing())
{
String str_htm="<html>"+(char)(13)+(char)(10)+
"<head>"+(char)(13)+(char)(10)+
"<title>操作成功</title>"+(char)(13)+(char)(10)+
"</head>"+(char)(13)+(char)(10)+
"<body bgcolor=\"#C0C0C0\">"+(char)(13)+(char)(10)+
"<SCRIPT language=JavaScript>"+(char)(13)+(char)(10)+
"self.close();"+(char)(13)+(char)(10)+
"</SCRIPT>"+(char)(13)+(char)(10)+
"</body>"+(char)(13)+(char)(10)+
"</html>";
printwriter.println(str_htm);
//writeOutput(str_htm,"c:\\inetpub\\wwwroot\\online\\img.htm");*/
}
}
// int i = smartupload.save("/");
// printwriter.println(i + " file uploaded.");
}
catch(Exception exception)
{
printwriter.println("Unable to upload the file.<br>");
printwriter.println("Error : " + exception.toString());
}
printwriter.println("</BODY>");
printwriter.println("</HTML>");
}
public final ServletConfig getServletConfig()
{
return config;
}
public final void init(ServletConfig servletconfig)
throws ServletException
{
config = servletconfig;
}
//流方式写文件
static void writeOutput(String str, String strOutFile) {
try {
FileOutputStream fos = new FileOutputStream(strOutFile);
Writer out = new OutputStreamWriter(fos, "GB2312");
out.write(str);
out.close();
}
catch (IOException e) {
e.printStackTrace();
e.printStackTrace();
}
}
//流方式读文件
static String readInput(String strInFile)//String AppendStr
{
StringBuffer buffer = new StringBuffer();
try {
FileInputStream fis = new FileInputStream(strInFile);
InputStreamReader isr = new InputStreamReader(fis, "GB2312");
Reader in = new BufferedReader(isr);
int ch;
int iCharNum;
iCharNum=0;
while ((ch = in.read())>-1) {
iCharNum += 1;
buffer.append((char)ch);
}
//buffer.append(AppendStr);
in.close();
return buffer.toString();
}
catch (IOException e) {
e.printStackTrace();
return null;
}
}
}