JSP使用jspsmartupload实现文件的上传

这个程序出现点问题,输入的只要是中文,都会出现乱码,自己经过各种各样的方法,还是无法解决,哪位大神能赐教一下……

:uploadFileForm.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>上传文件</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>

<body>
<form method="post" action="uploadFileAccept.jsp" name="book"
enctype="multipart/form-data">
<table align="center" cellspacing="0" cellpadding="3" border="1" width="474">
<tr>
<td align="center" colspan="2">录入书籍的信息</td>
</tr>
<tr>
<td width="150">书名:</td>
<td width="324"><input type="text" name="bookname"></td>
</tr>
<tr>
<td>封面:</td>
<td><input type="file" name="photofile">
</td>
</tr>
<tr>
<td>作者:</td>
<td><input type="text" name="author">
</td>
</tr>
<tr>
<td>出版社:</td>
<td><input type="text" name="publisher">
</td>

</tr>
<tr>
<td colspan="2" width="474"><input type="submit" value="提交">
</td>
</tr>
</table>


</form>
</body>
</html>

 

 

 

:uploadFileAccept.jsp

<%@page import="com.jspsmart.upload.SmartUpload"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>接收文件的上传处理和显示</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->


</head>

<body>
<%--public String codeToString(String str) {
String s = str;
try {
byte[] arr = s.getBytes("gbk");
s = new String(arr);
return s;
} catch (Exception e) {

return s;
}
}--%>


<%
//request.setCharacterEncoding("GBK");
//新建一个smartupload对象
SmartUpload su = new SmartUpload();
//上传初始化,pageContext为jsp的内置对象
su.initialize(pageContext);
//设置允许上传的文件的类型,思路是通过拓展名的限制
su.setAllowedFilesList("jpg,gif");
//执行上传文件动作
su.upload();
//将上传的文件全部保存到指定的目录
su.save("upload", SmartUpload.SAVE_VIRTUAL);
%>
<table cellspacing="0" cellpadding="3" border="1" width="474">
<tr>
<td width="150">书名:</td>
<td width="324">
<%--
byte[] book_name = (su.getRequest().getParameter("bookname")).getBytes("GBK");
String bookname=new String(book_name, "GBK");
out.print(bookname);
--%>
<%=su.getRequest().getParameter("bookname") %>
</td>

</tr>
<tr>
<td>封面:</td>
<td><img
src="<%="upload/" + su.getFiles().getFile(0).getFileName()%>">
</td>

</tr>
<tr>
<td>作者</td>
<td><%=su.getRequest().getParameter("author")%></td>
</tr>
<tr>
<td>出版社:</td>
<td><%=su.getRequest().getParameter("publisher")%></td>
</tr>
</table>
</body>
</html>

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