关于jspSmartUpload的上传问题,急~~~~在线等,求求各位帮帮忙

wwwfox111 2004-09-04 03:44:22
我现在在做一个上传的应用,使用的是jspSmartUpload组件,上传的表单如text如果是中文,上传后存到数据库是乱马,我尝试了很多种转码方式结果出现的是各式各样的乱马,没法解决,只好求助于各位大哥大姐了。在线等。
环境是tomcat4.1+sql2000

//提交表单
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>无标题文档</title>
</head>
<body>
<div align="center">
<table width="400" border="0">
<tr>
<td><form action="upload.jsp" method="post" enctype="multipart/form-data" name="form1">
<div align="center">
<input name="path" type="hidden" id="path" value="news">
标题:<input name="title" type="text" id="title">
<input name="type" type="hidden" id="type" value="1">
<br>
文件:<input type="file" name="file">
<input type="submit" name="Submit" value="提交">
</div>
</form></td>
</tr>
</table>
</div>
</body>
</html>

\\上传页面
<link href="../css/style.css" rel="stylesheet" type="text/css"> <div align="center"></div>
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*"%>
<%@ page errorPage="error.jsp"%>
<%@ page import="com.jspsmart.upload.*" %>
<%@ page import="hetronic.lib.*"%>
<%@ page session="true"%><jsp:useBean id="my" scope="request" class="hetronic.lib.Upload"/>
<%! String mesg=""; %>
<%
//实例化上载bean
com.jspsmart.upload.SmartUpload mySmartUpload=new com.jspsmart.upload.SmartUpload();
//初始化
mySmartUpload.initialize(pageContext);
//设置上载的最大值
mySmartUpload.setMaxFileSize(50*1024*1024);
mySmartUpload.setTotalMaxFileSize(50*1024*1024);
mySmartUpload.setAllowedFilesList("pdf,PDF,rar,RAR,doc");

//上载文件
mySmartUpload.upload();
//获取表单
String path=(String)mySmartUpload.getRequest().getParameter("path");
String name=(String)mySmartUpload.getRequest().getParameter("name");
String type=(String)mySmartUpload.getRequest().getParameter("type");
String filename="";
//循环取得所有上载的文件
//取得上载的文件
for (int i=0;i<mySmartUpload.getFiles().getCount();i++){
com.jspsmart.upload.File myFile = mySmartUpload.getFiles().getFile(i);
if (!myFile.isMissing())
{
//取得上载的文件的文件名
//取得不带后缀的文件名
//取得后缀名
String ext= mySmartUpload.getFiles().getFile(i).getFileExt();
//取得文件的大小
int fileSize=myFile.getSize();
//保存路径
String aa=getServletContext().getRealPath("/")+"file/"+path+"\\";
String trace=aa+name+i+"."+ext;
filename=filename+name+i+"."+ext+",";
//将文件保存在服务器端
myFile.saveAs(trace);
}
}
String sqlStr="insert into oyw_News(title,type,content)values(?,?,?)";
try{
PreparedStatement ptmt=LinkDataBase.getPreparedStatement(sqlStr);
ptmt.setString(1,name);
ptmt.setInt(2,Integer.parseInt(type));
ptmt.setString(3,filename);
ptmt.execute();
ptmt.close();
LinkDataBase.close();
}catch(Exception e){
}
%>



...全文
358 11 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
yaray 2004-09-15
  • 打赏
  • 举报
回复
下载地址:http://www.mmbase.org/download/builds/2003-05-01/jspsmartupload.jar
huafong 2004-09-15
  • 打赏
  • 举报
回复
Listing 7. The corrected version for file download
<%
try
{
System.String filename = "C:\\downloadJSP\\myFile.txt";

// set the http content type to "APPLICATION/OCTET-STREAM
Response.ContentType = "APPLICATION/OCTET-STREAM";

// initialize the http content-disposition header to
// indicate a file attachment with the default filename
// "myFile.txt"
System.String disHeader = "Attachment;
Filename=\"myFile.txt\"";
Response.AppendHeader("Content-Disposition", disHeader);

// transfer the file byte-by-byte to the response object
System.IO.FileInfo fileToDownload = new
System.IO.FileInfo(filename);
System.IO.FileStream fileInputStream = new
System.IO.FileStream(fileToDownload.FullName,
System.IO.FileMode.Open, System.IO.FileAccess.Read);
int i;
while ((i = fileInputStream.ReadByte()) != - 1)
{
Response.Write((char)i);
}
fileInputStream.Close();
Response.Flush();
Response.Close();
}
catch (System.Exception e)
// file IO errors
{
SupportClass.WriteStackTrace(e, Console.Error);
}
%>
JLCA Conversion of File Upload
Because the majority of Java-based Web applications use third-party software to control the uploading of files, the JLCA will not convert most of your file uploading code, requiring you to do the re-writing manually. Depending on your third-party software, you may also have to redesign portions of your site. For example, on the CodeNotes site, we used an ActionForm bean with a FormFile property to represent uploaded files. Because we can no longer use Struts in the .NET version of the CodeNotes site, we not only had to replace the FormFile-related code, we also had to redesign the way the HTML form was processed, replacing the Struts code that automatically updated the form. However, as we will soon discuss, the HttpRequest class makes it very easy to retrieve uploaded files and the HttpPostedFile class provides all the functionality we required of the FormFile class, so the changes that were required were neither difficult nor lengthy to implement.
ASP.NET File Download
Downloading a file in ASP.NET is straightforward and does not require you to copy the file byte-by-byte to the response page, although, as shown in Listing 7, byte-by-byte transfer is still possible if you require the contents of the file to be changed at run-time. Not having to copy the file byte-by-byte will eliminate the casting problems we encountered in the JLCA Conversion of File Download section. You must still set the http content type to indicate the file is for downloading, but instead of transferring the file byte-by-byte, you can simply call the HttpResponse.WriteFile() method to transfer the file as shown in Listing 8.
Listing 8. Downloading a file in ASP.NET
<%
try
{
System.String filename = "myFile.txt";

// set the http content type to "APPLICATION/OCTET-STREAM
Response.ContentType = "APPLICATION/OCTET-STREAM";

// initialize the http content-disposition header to
// indicate a file attachment with the default filename
// "myFile.txt"
System.String disHeader = "Attachment; Filename=\"" + filename +
"\"";
Response.AppendHeader("Content-Disposition", disHeader);

// transfer the file byte-by-byte to the response object
System.IO.FileInfo fileToDownload = new
System.IO.FileInfo("C:\\downloadJSP\\DownloadConv\\myFile.txt");
Response.Flush();
Response.WriteFile(fileToDownload.FullName);}
catch (System.Exception e)
// file IO errors
{
SupportClass.WriteStackTrace(e, Console.Error);
}
%>
Notice that in Listing 8 as in Listing 1 there are still three basic steps to downloading a file. First, the http content type is set to APPLICATION/OCTET-STREAM. Second, the Content-Disposition header is set to indicate an attachment and the default file name. However, unlike Listing 1 in the third step, the file is not sent byte-by-byte, but instead a single line sends the file contents.
ASP.NET File Upload
Uploading a file in ASP.NET is very straightforward. You have to add the File Field html server control to your Web form. Once you have added the File Field control to your HTML form you need to make sure that the form has the method="post" and enctype="multipart/form-data" attributes in order to be able to access the posted file. Listing 9 shows a sample form that accepts a single file from the user and a button to submit the form.
Listing 9. A form for uploading a file
<form id="FileUploadForm" method="post"
enctype="multipart/form-data" action="afterUpload.aspx">
<P><INPUT type="file" id="UploadedFile" runat="server"></P>
<P><INPUT type="submit" value="Submit"></P>
</form>
Once the user has added the file to the form and clicked the Submit button, you can access the posted file through the HttpRequest object. All uploaded files for the current request are stored in the HttpRequest.Files property. The Files property is an HttpFileCollection object that acts similarly to a Hashtable: a String is used as a key and the value returned is an HttpPostedFile object. The HttpPostedFile class provides similar functionality to that provided by the FormFile class in Struts. This functionality is provided through various properties in the HttpPostedFile class such as the FileName property, which returns the uploaded file's name, including directories, on the client's computer; the InputStream property, which returns a Stream object containing the file data; and the ContentLength property which returns the length in bytes of the uploaded file. Unlike the FormFile class, the HttpPostedFile class also contains a SaveAs() method which stores the file on your server with the given file name. Listing 10 contains an example of how to use the HttpPostedFile class to save an uploaded file to disk using the original file name. Because this code requires access to the HttpRequest object containing information about the form, this code will be placed in the Page_Load() method of the next ASP.NET page or any other code that will be executed after the form has been submitted, but before the next ASP.NET page is displayed. For example, the code in Listing 10 could be placed in the Page_Load() method of the afterUpload.aspx page, which is referenced by the action attribute in Listing 9.
Listing 10. Saving an uploaded file to disk
HttpFileCollection allFiles = Request.Files;
HttpPostedFile uploadedFile = allFiles["UploadedFile"];
FileInfo uploadedFileInfo = new
FileInfo(uploadedFile.FileName);
uploadedFile.SaveAs("C:\\UploadedFiles\\" +
uploadedFileInfo.Name);
userMessage = "The file has been svaed with the filename: " +
uploadedFileInfo.Name;
Summary
The basic process for a file download (from server to client) can be the same for both JSP and ASP.NET. In fact, the JLCA will properly convert your JSP code with a few minor changes. However, the process for uploading a file may be totally different, especially if you are using Struts on the JSP side. ASP.NET has a built-in control specifically for uploading files that replaces the Struts functionality. In other words, with a file upload, you have a case where replacing JSP code with a built in ASP.NET feature is faster, easier and better than trying to make a conversion work properly.
huafong 2004-09-15
  • 打赏
  • 举报
回复
Listing 4. A Web form for uploading a file
<%@page language="java"%>
<%@taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>

<html:html>
<html:form enctype="multipart/form-data"action="/upload">

<h1>Add Article</h1>
Please enter file you wish to add:
<br><br>
<html:file property="uploadedFile"/>
<br><br>
<html:submit value="Upload File"/>
</html:form>

</html:html>
Notice that in Listing 4, we used the prefix html: to refer to all elements from the Struts html tag library. This allowed us to create a Struts HTML form element by using html:form and a Struts HTML file element using html:file.
Now in our UploadAction class, we can use the FormBean to retrieve information from the file uploaded to the form in Listing 4. However, as shown in Listing 5 we must first cast the form parameter to an UploadedFileForm in the execute() method of the UploadAction class. The Struts FormFile class allows you to perform various functions to retrieve information from the uploaded file through methods such as: getFileSize(), getInputStream(), and getFileName(). In the UploadAction class in Listing 5, we use the getFileName() and getInputStream() to save the file from the form in Listing 4 to disk.
Listing 5. Saving an uploaded file to disk using Struts
public ActionForward
perform(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {

UploadedFileForm fileForm = (UploadedFileForm)form;
FileForm uploadedFile = fileForm getUploadedFile();
InputStream uploadInStream = uploadedFile.getInputStream();
String filename = uploadedFile.getFileName();
FileOutputStream fOut = new FileOutputStream("c:\\uploads\\"
+ filename);
int c=0;
while (c=uploadInStream.read() != -1) {
fOut.write(c);
} // while
fOut.flush();
fOut.close();
// perform the rest of the action here
}
JLCA Conversion of File Download
When the JLCA converts a JSP page containing file-downloading code, the resultant Microsoft ASP.NET page will be very close to being functionally equivalent. For our sample JSP page, the JLCA produced the ASP.NET code shown in Listing 6.
Listing 6. The JLCA produced .aspx page for file download
<%
try
{
System.String filename = "C:\\downloadJSP\\myFile.txt";

// set the http content type to "APPLICATION/OCTET-STREAM
Response.ContentType = "APPLICATION/OCTET-STREAM";

// initialize the http content-disposition header to
// indicate a file attachment with the default filename
// "myFile.txt"
System.String disHeader = "Attachment;
Filename=\"myFile.txt\"";
Response.AppendHeader("Content-Disposition", disHeader);

// transfer the file byte-by-byte to the response object
System.IO.FileInfo fileToDownload = new
System.IO.FileInfo(filename);
System.IO.FileStream fileInputStream = new
System.IO.FileStream(fileToDownload.FullName,
System.IO.FileMode.Open, System.IO.FileAccess.Read);

int i;
while ((i = fileInputStream.ReadByte()) != - 1)
{
Response.Write(i);
}
fileInputStream.Close();
Response.Flush();
Response.Close();
}
catch (System.Exception e)
// file IO errors
{
SupportClass.WriteStackTrace(e, Console.Error);
}
%>
Only one line needed to be changed in the converted code in order for it to work properly. The bolded line in Listing 6 needed to be changed from Response.Write(i) to Response.Write((char)i). This is because we are transferring text files for download and without casting to a char, the downloaded file was containing the ASCII codes for the characters as opposed to the characters themselves. Even if you are not making text files available for download, you may have to change the way the file is written byte-by-byte, so it is important that you test your file upload application for each possible file that may be downloaded. The corrected file-transfer .NET code is shown in Listing 7.
huafong 2004-09-15
  • 打赏
  • 举报
回复
Downloading and Uploading Files
Microsoft Corporation
October 2003
Applies to:
Microsoft® ASP.NET
Java Server Pages (JSP)
Summary: Learn common ways of downloading and uploading files in Java and how the Java Language Conversion Assistant (JLCA) converts Java file-handling code from Java to C#. See how JLCA-produced code can be modified to take advantage of .NET features not present in Java. (11 printed pages)
Contents
Introduction
File Download In Java
File Upload in Java
JLCA Conversion of File Download
JLCA Conversion of File Upload
ASP.NET File Download
ASP.NET File Upload
Summary
Introduction
Most Web applications involve file transfers between the client and the server. On the CodeNotes site, users can download the source code contained in various CodeNotes books, and administrators can add articles (stored as XML files) to the site for viewing by clients. In this article, we'll examine common ways of downloading and uploading files in Java. We will then examine how (or whether) the JLCA converts the file-handling code from Java to C#. Finally, we'll look at how you can modify the JLCA-produced code to take advantage of Microsoft® .NET features that are not present in Java.
Throughout this article, we will refer to file transfers from the client's perspective, so the phrase downloading a file should be interpreted as the user retrieving a file from your Web application, and the phrase uploading a file should be interpreted as the user posting a file to your Web application.
File Downloading in Java
File downloading in Java requires you to write code to send the file to the user byte-by-byte through the HttpServletResponse object. In addition to sending the file, you must also change the http content type and http header to indicate the type and name of the file that the client will be downloading.
The following example is a common practice of file downloading in a JSP page.
Listing 1. Downloading a file in Java
<% try {
String filename = "C:\\downloadJSP\\myFile.txt";

// set the http content type to "APPLICATION/OCTET-STREAM
response.setContentType("APPLICATION/OCTET-STREAM");

// initialize the http content-disposition header to
// indicate a file attachment with the default filename
// "myFile.txt"
String disHeader = "Attachment;Filename=\"myFile.txt\"";
response.setHeader("Content-Disposition", disHeader);

// transfer the file byte-by-byte to the response object
File fileToDownload = new File(filename);
FileInputStream fileInputStream = new
FileInputStream(fileToDownload);
int i;
while ((i=fileInputStream.read())!=-1)
{
out.write(i);
}
fileInputStream.close();
out.close();
}catch(Exception e) // file IO errors
{
e.printStackTrace();
}
%>
Notice that in Listing 1 there are three main steps. First, we use the response.setContentType() method to set the MIME type of the response page to APPLICATION/OCTET-STREAM. Second, we set the Content-Disposition http header to provide the default filename of myFile.txt using the response.setHeader() method. Third, we open the file and add its contents, byte-by-byte, to the response page using the out.write() method. When the client is transferred to a JSP page containing the code in Listing 1 they will be presented with the File Download dialog for their browser and be prompted whether or not they wish to save or open myFile.txt.
File Upload in Java
The J2EE and J2SE libraries do not provide direct support for uploading files, so file uploading in Java is most often controlled through third-party libraries. On the CodeNotes site, we used the Struts library to control the uploading of files by the client. Specifically, we used a FormFile and a FormBean to control file uploading. A FormBean is created the same way as a basic JavaBean, except it must extend the Struts ActionForm class. Creating a FormBean that stores a FormFile object requires you to add setXXX and getXXX methods for accessing the field that will store the uploaded file. Listing 2 shows a simple FormBean that has the single field, uploadedFile representing a FormFile.
Listing 2. An ActionForm bean containing a single FormFile object
package com.codenotes.upload;

import org.apache.struts.upload.FormFile;
import org.apache.struts.action.ActionForm;

public class UploadedFileForm extends ActionForm {
FormFile uploadedFile;

public void setUploadedFile(FormFile file) {
uploadedFile = file;
} // setUploadedFile

public FormFile getUploadedFile() {
return uploadedFile;
} // getUploadedFile
} // UploadedFileForm
As previously mentioned, we must also change the struts-config.xml file to associate the FormBean with a Struts action. Two changes must be made to the struts-config.xml file, as shown in Listing 3. First, we must add a form-bean element to the <form-beans> element. This new <form-bean/> element will provide the class name of our FormBean and the name we will use to reference the FormBean throughout the rest of struts-config.xml. Second, we have to associate the FormBean with a struts action. In this example, we will associate the FormBean with the UploadAction class, which we will partially define near the end of this section.
Listing 3. Associating a FormBean with an action in the struts-config.xml file
<!-- start of struts-config.xml omitted for brevity -->
<form-beans>
<!-- provide the name and classname for our FormBean -->
<form-bean name="uploadForm"
type="com.codenotes.upload.UploadedFileForm"/>
</form-beans>

<action-mappings>
<!-- associate the form with the UploadAction class which
we will define later. -->
<action path="/upload"
name="uploadForm"
scope="session"
type="com.codenotes.upload.UploadAction">
<forward name="uploadResult" path="/jsp/uploadResult.jsp"/>
</action>
<!-- the rest of the struts-config is omitted for brevity -->
The bolded lines indicate that we have associated the FormBean with the name uploadForm to the /upload action. Because of the form-bean element that we added at the beginning of the struts-config.xml file, uploadForm is mapped to an instance of the UploadedFileForm class defined in Listing 2. Setting the scope attribute to session indicates that the values for this FormBean should be retained throughout the client's entire visit to the site. The alternative value for scope is request, which will only retain values for the bean during the individual client request.
Once the FormBean has been associated with an action, we need to use the Struts html tag library in our JSP page in order to have the struts engine update the FormBean. In order to map the entries of the HTML form to the properties in the FormBean, the form element, and all its associated input elements, must be created using the Struts HTML tag library's form element, and the respective Struts HTML input elements. A sample form using the UploadedFileForm is shown in Listing 4.
nethare 2004-09-15
  • 打赏
  • 举报
回复
我也是使用这个组件,没有这个问题啊
yehuo 2004-09-15
  • 打赏
  • 举报
回复
d
wwwfox111 2004-09-04
  • 打赏
  • 举报
回复
得到表单对象值是通过smartupload.getRequest().getParameter()方法。

我在页面上加入<%request.setCharacterEncoding("gb2312");%>
问题还是存在!

大家再帮我仔细看看了。
gsen 2004-09-04
  • 打赏
  • 举报
回复
up
boby1863 2004-09-04
  • 打赏
  • 举报
回复
在页面加上<%request.setCharacterEncoding("gb2312");%>
乱码问题不难解决呀..
power17 2004-09-04
  • 打赏
  • 举报
回复
看了半天,终于知道你的程序是想要干什么了的。真是不容易啊。这个我以前写过了,也调

试好了。你稍微改一下就可以用了。
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>无标题文档</title>
</head>

<body>
<table width="50%" border="1" align="center" cellpadding="0" cellspacing="1">
<tr>
<td><div align="center">
<form name="form1" method="post" action="upload_sql.jsp" enctype="multipart/form-data">
<p>file1:
<input name="file1" type="file" id="file1">
</p>
<p>
file2:
<input name="file2" type="file" id="file4">
</p>
<p>
<input type="submit" name="Submit" value="提交">
</p>
</form>
</div></td>
</tr>
</table>
</body>
</html>







jsp:

<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" import="java.util.*,com.jspsmart.upload.*" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>无标题文档</title>
</head>

<body>
<%!
//声明变量
String DRV="com.microsoft.jdbc.sqlserver.SQLServerDriver";
String URL="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=web";
String USER="sa";
String PWD="";
Connection conn=null;
%>
<%
//加载驱动
try{
Class.forName(DRV);
}catch(Exception e){
out.println("cannot load drive.");
};
//建立与数据库的连接
try{
conn=DriverManager.getConnection(URL,USER,PWD);
}catch(Exception e){
out.println("cannot connect to database.");
};

%>
<p>

<%! int i=0;%>
<%
//开始实现文件的上传
SmartUpload su=new SmartUpload();
//su.setMaxFileSize(4096);
su.initialize(pageContext);
su.upload();
%>
</p>
<p><h1><font color="#FF0000">上传成功。</font></h1>
</p>
<p>上传文件信息如下:</p>
<p>
<%
try{
Files files=su.getFiles();
int count=files.getCount();
out.println("<table border="1" cellspacing="10" cellpadding="14"><tr>");
out.println("<td>formtter name</td><td>size of the file</td>path<td>upload name</td><td>upload name</td></tr><tr>");
for(int i=0;i<count;i++){
com.jspsmart.upload.File file=files.getFile(i);
if(file.isMissing()){
continue;
}
file.saveAs("upload/"+file.getFileName()+"."+file.getFileExt());
out.println("<td>"+file.getFileName()+"</td>");
out.println("<td>"+file.getSize()+"</td>");
out.println("<td>"+file.getFilePathName()+"</td>");
out.println("<td>"+file.getFileName()+"</td></tr>");
String sqlStmt="insert into pic_upload values('"+file.getFileName()+"','"+file.getFilePathName()+"')";

//上传有关图片的信息,即图片的路径
if(conn!=null){
PreparedStatement stmt=conn.prepareStatement
(sqlStmt,ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
int k=stmt.executeUpdate();
}
}
out.println("</table>");}catch(Exception e){out.println("上传发生错误。");}

%>
</p>
<p> </p>
</body>
</html>
cocohui 2004-09-04
  • 打赏
  • 举报
回复
大哥你有这jspSmartUpload组件吗??给个给我吧 。。我找了好就都没有找到。。

故我没有方法帮你。我就在这里帮你顶!!

cocohui2002@163.com

81,122

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧