jspsmartupload上传下载,解决乱码

如题,这个上传下载功能用的是jspsmartupload组件,要到网上下载jar包

先上一个简单的案例程序:

上传:

前台:

<%@ page contentType="text/html;charset=UTF-8"%>
<html>
<head>
<title>jspSmartUpload组件文件上传</title>
</head>
<body>
	<br>
	<br>
	<center>
		<font size="5" color="#ff0000"><b>jspSmartUpload组件文件上传</b> </font><br>
		<form name="uploadForm" enctype="multipart/form-data" method="post"
			action="servlet/SmartUploadServlet">
			<table border="0" align="center" cellpadding="2" cellspacing="2"
				bgcolor="snow">
				<input type="hidden" name="paramete" value="OK!">
				<tr>
					<td>
						<div align="center">上传文件1:</div>
					</td>
					<td><div align="center">
							<input type="file" name="file1" size="25" maxlength="80">
						</div></td>
				</tr>
				<tr>
					<td>
						<div align="center">上传文件2:</div>
					</td>
					<td><div align="center">
							<input type="file" name="file2" size="25" maxlength="80">
						</div></td>
				</tr>
				<tr width="100%">
					<td>
						<div align="center">
							<input type="submit" value="上传文件"> <input type="reset"
								value="清除">
						</div>
					</td>
				</tr>
			</table>
		</form>
	</center>
</body>
</html>

后台:

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jspsmart.upload.SmartUpload;
public class SmartUploadServlet extends HttpServlet {
 private ServletConfig config;
 //初始化Servlet
 final public void init(ServletConfig config)
   throws ServletException{
  this.config=config;
 }
 //处理GET请求
 public void doGet(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {

  this.doPost(request, response);
 }
 //响应POST请求
 public void doPost(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  //获取PrintWriter对象
  PrintWriter out = response.getWriter();
  out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
  out.println("<HTML>");
  out.println("<HEAD><TITLE>A Servlet</TITLE></HEAD>");
  out.println("<BODY bgcolor=‘white‘><center><br>");
  out.println("<br><h1>jspSmartUpload</h1>");
  out.println("<hr><br>");
  //新建一个SmartUpload对象
  SmartUpload mySmartUpload=new SmartUpload();
  try{
   //上传初始化
   mySmartUpload.initialize(config, request, response);
   //设定每个上传文件的最大长度
   mySmartUpload.setMaxFileSize(1*512*1024);
   //设定总上传数据的长度
   mySmartUpload.setTotalMaxFileSize(1*1024*1024);
   //设定允许上传的文件的类型,只允许上传java,doc,txt文件
   mySmartUpload.setAllowedFilesList("java,doc,txt");
   //设定禁止上传的文件的类型,禁止上传带有exe,bat文件
   mySmartUpload.setDeniedFilesList("exe,bat");
   //上传文件
   mySmartUpload.upload();
   //将上传文件全部保存到指定目录
   String name = new String(mySmartUpload.getRequest().getParameter("paramete"));
   System.out.println(name);
   int count=mySmartUpload.save("/");
   out.println(//利用Request对象获取参数之值
     //String name= new String(request.getParameter("name").getBytes("ISO-8859-1"),"utf-8") ; 
     mySmartUpload.getRequest().getParameter("paramete")+" ");
   out.println(name);
   //显示处理结果
   out.println("<font color=red>"+count+"</font> File Upload!<br>");
   //处理每个上传文件
   for(int i=0;i<mySmartUpload.getFiles().getCount();i++)
   {
    com.jspsmart.upload.File file
    =mySmartUpload.getFiles().getFile(i);
    //判断用户是否选择了文件
    if(!file.isMissing()){
     //打印文件名
     out.println("File Name: <font color=red>"
       +file.getFileName()+"</font><br>");
     //打印文件扩展名
     out.println("File ExtName: <font color=red>"
       +file.getFileExt()+"</font><br>");
     //打印文件路径,包括目录
     out.println("Path: <font color=red>"
       +file.getFilePathName()+"</font><br>");
     //另存到以Web应用程序的根目录为文件根目录的目录下
     //(声明一下:在Myeclipse中,该目录位于工程下的.metadata/.me_tcat/webapps/该工程目录/upload/)
     file.saveAs("/upload/"+file.getFileName(),mySmartUpload.SAVE_VIRTUAL);
     
    }
   }
  }catch(Exception e){//异常处理
   //打印自定义异常信息
   out.println("Unanable to upload the file.<br>");
   out.println("Please Check The File Type");
  }
  
 }
}


下载:

前台:

<%@ page contentType="text/html;charset=UTF-8"%>
<html>
<head>
<title>jspSmartUpload组件下载文件</title>
</head>
<body>
	<br>
	<br>
	<center>
		<font size="5" color="#ff0000"><b>jspSmartUpload组件下载文件</b>
		</font><br>
		<form name="uploadForm" enctype="multipart/form-data" method="post"
			action="servlet/SmartDownloadServlet">
			<table border="0" align="center" cellpadding="2" cellspacing="2"
				bgcolor="snow">
				<tr>
					<td>
						<div align="center">
							<a href="servlet/SmartDownloadServlet?fileName=新建文本文档.txt">文件1</a>
						</div></td>
					<td>
						<div align="center">
							<a href="servlet/SmartDownloadServlet?fileName=2.jpg">文件2</a>
						</div></td>
				</tr>
			</table>
		</form>
	</center>
</body>
</html>


后台:

import java.io.IOException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jspsmart.upload.SmartUpload;
public class SmartDownloadServlet extends HttpServlet {
	private ServletConfig config;
	//初始化Servlet
	final public void init(ServletConfig config)
			throws ServletException{
		this.config=config;
	}
	//处理GET请求
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}
	//响应POST请求
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		//新建一个SmartUpload对象
		SmartUpload mySmartUpload=new SmartUpload();
		try{
			//上传初始化
			mySmartUpload.initialize(config, request, response);
			//String name= new String(request.getParameter("name").getBytes("ISO-8859-1"),"utf-8") ; 
			String fileName=new String(request.getParameter("fileName").getBytes("ISO-8859-1"),"utf-8");
			//System.out.println(fileName);
			//设定contentDisposition为null以禁止浏览器
			//自动打开文件
			//保证单击链接后是下载文件,若不设定,则
			mySmartUpload.setContentDisposition(null);
			//下载文件 可加路径"/upload/"
			
			mySmartUpload.downloadFile("/upload/"+fileName);
		}catch(Exception e){//异常处理
		}
	}
}


要注意一点,如果用了这个组件,那么上传的form表单中,要加一个属性:enctype="multipart/form-data"  ;而且因为此,如果要同时传其他参数,后台接收的时候不能用以前的request.getParamter(); 而是要用这个组件里面的方法:比如你同时在input中写了文件名docName,要传这个值,后台要在mySmartUpload.upload();这句后写String docName = mySmartUpload.getRequest().getParameter("doNname"); ,注意一定要在那句话之后。

现在问题来了,这个组件确实用起来非常简单,在一些小的项目中很实用,但他也有非常让人头疼的问题——中文乱码。这个问题也没有什么好的解决办法,因为jspsmartupload这个组件本来就不支持中文,唯一的一个解决办法就是改他的源码。你可以尝试上网找办法自己改一下,很麻烦的。现笔者已将此问题解决。完美支持jspsmartupload中文上传下载及传参的jar包如下:

http://download.csdn.net/detail/u011250851/7200253  希望能给你帮助。

jspsmartupload上传下载,解决乱码,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。