jspsmartupload 乱码问题和文件上传下载
2018-03-27 23:23
1481 查看
最后乱码空指针问题
表单中提交中text内容request.getParameter("")获取为空
解决方法:SmartUpload su = new SmartUplad();
su.getRequest.getParameter("");如果页面编码格式为utf-8,但这种获得结果中文乱码 ,如果页面编码格式为gbk,获取不乱码。页面编码UTF-8,已下方法解决中文乱码, new String(su.getRequest.getParameter("").getBytes(),"UTF-8");
文件上传下载:
1.jsp页面代码
test.jsp:<form action="<%=path%>/TestFileUpload.do" enctype="multipart/form-data" method="post">
<input type="file" name="file1" size="30" />
<input type="file" name="file2" />
<input type="file" name="file3"/>
<input type="submit" value="上传"/>
</form>
<a href="<%=path %>/TestFileDownload.do?fileName=test.txt" > 下载文件test.txt</a>2.servlet代码
TestFileUpload文件上传servlet类中doPost方法代码:protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// 上传服务器下文件路径
String filePath = "D:/weixinimages/nginx/html";
Writer out = resp.getWriter();
if (FileUploadUtil.jspSmartUploadFile(this.getServletConfig(), req,
resp, filePath)) {
out.write("<h>上传成功!!!</h>");
} else {
out.write("<h>上传失败!!!</h>");
}
out.flush();
out.close();
}TestFileDownload文件下载servelt中doGet方法代码: String fileName = req.getParameter("fileName");
// 下载文件路径
String filePath = "D:/weixinimages/nginx/html/" + fileName;
// 上传结果
String result = "";
if (FileUploadUtil.jspSmartDownloadFile(
this.getServletConfig(), req,resp, filePath)) {
result = "---上传成功";
} else {
result = "---上传失败";
}
req.setAttribute("result", result);两个servlet中调用了FileUploadUtil工具类中两个方法代码:/**
*
*(描述:) jspSmartUpload 上传文件 form表单 enctype="multipart/form-data"
* @method jspSmartUpload
* @param sc
* @param req
* @param resp
* @param filePath
* void
*/
public static boolean jspSmartUploadFile (ServletConfig sc,
HttpServletRequest req, HttpServletResponse resp ,String filePath){
boolean flag = true;
// 创建文件路径 如:D:/weixinimages/nginx/html
File file = new File(filePath);
if(!file.exists()){
file.mkdir();
}
try{
// 初始化对象
su.initialize(sc, req, resp);
// 设置上传文件大小不为5M
su.setMaxFileSize(1024*1024*5);
// SmartUpload 为多文件上传下载处理第三方组件
// 设置所有文件上传最大大小 50M
su.setTotalMaxFileSize(1024*1024*50);
//设置允许上传文件类型
su.setAllowedFilesList("jpg,png,gif");
// 拒绝上传文件内容
su.setDeniedFilesList("rar,jsp,js,css");
su.upload();
// su.save 返回类型为int 为保存文件数
int cout = su.save(filePath);
logger.info("成功上传" + cout+"张图片!!!");
}catch(Exception e){
e.printStackTrace();
flag = false;
}
return flag;
}
/**
*
*(描述:) jspsmartupload 下载文件
* @method jspSmartDownloadFile
* @param sc
* @param req
* @param resp
* @param filePath
* @return
* boolean
*/
public static boolean jspSmartDownloadFile(ServletConfig sc,
HttpServletRequest req, HttpServletResponse resp ,String filePath){
boolean flag = true;
try {
// 初始化
su.initialize(sc, req, resp);
// 设置contentDisposition为null 禁止浏览器打开文件
su.setContentDisposition(null);
su.downloadFile(filePath);
} catch(Exception e) {
e.printStackTrace();
flag = false;
}
return flag;
}
最后附上下载文件效果图:
表单中提交中text内容request.getParameter("")获取为空
解决方法:SmartUpload su = new SmartUplad();
su.getRequest.getParameter("");如果页面编码格式为utf-8,但这种获得结果中文乱码 ,如果页面编码格式为gbk,获取不乱码。页面编码UTF-8,已下方法解决中文乱码, new String(su.getRequest.getParameter("").getBytes(),"UTF-8");
文件上传下载:
1.jsp页面代码
test.jsp:<form action="<%=path%>/TestFileUpload.do" enctype="multipart/form-data" method="post">
<input type="file" name="file1" size="30" />
<input type="file" name="file2" />
<input type="file" name="file3"/>
<input type="submit" value="上传"/>
</form>
<a href="<%=path %>/TestFileDownload.do?fileName=test.txt" > 下载文件test.txt</a>2.servlet代码
TestFileUpload文件上传servlet类中doPost方法代码:protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// 上传服务器下文件路径
String filePath = "D:/weixinimages/nginx/html";
Writer out = resp.getWriter();
if (FileUploadUtil.jspSmartUploadFile(this.getServletConfig(), req,
resp, filePath)) {
out.write("<h>上传成功!!!</h>");
} else {
out.write("<h>上传失败!!!</h>");
}
out.flush();
out.close();
}TestFileDownload文件下载servelt中doGet方法代码: String fileName = req.getParameter("fileName");
// 下载文件路径
String filePath = "D:/weixinimages/nginx/html/" + fileName;
// 上传结果
String result = "";
if (FileUploadUtil.jspSmartDownloadFile(
this.getServletConfig(), req,resp, filePath)) {
result = "---上传成功";
} else {
result = "---上传失败";
}
req.setAttribute("result", result);两个servlet中调用了FileUploadUtil工具类中两个方法代码:/**
*
*(描述:) jspSmartUpload 上传文件 form表单 enctype="multipart/form-data"
* @method jspSmartUpload
* @param sc
* @param req
* @param resp
* @param filePath
* void
*/
public static boolean jspSmartUploadFile (ServletConfig sc,
HttpServletRequest req, HttpServletResponse resp ,String filePath){
boolean flag = true;
// 创建文件路径 如:D:/weixinimages/nginx/html
File file = new File(filePath);
if(!file.exists()){
file.mkdir();
}
try{
// 初始化对象
su.initialize(sc, req, resp);
// 设置上传文件大小不为5M
su.setMaxFileSize(1024*1024*5);
// SmartUpload 为多文件上传下载处理第三方组件
// 设置所有文件上传最大大小 50M
su.setTotalMaxFileSize(1024*1024*50);
//设置允许上传文件类型
su.setAllowedFilesList("jpg,png,gif");
// 拒绝上传文件内容
su.setDeniedFilesList("rar,jsp,js,css");
su.upload();
// su.save 返回类型为int 为保存文件数
int cout = su.save(filePath);
logger.info("成功上传" + cout+"张图片!!!");
}catch(Exception e){
e.printStackTrace();
flag = false;
}
return flag;
}
/**
*
*(描述:) jspsmartupload 下载文件
* @method jspSmartDownloadFile
* @param sc
* @param req
* @param resp
* @param filePath
* @return
* boolean
*/
public static boolean jspSmartDownloadFile(ServletConfig sc,
HttpServletRequest req, HttpServletResponse resp ,String filePath){
boolean flag = true;
try {
// 初始化
su.initialize(sc, req, resp);
// 设置contentDisposition为null 禁止浏览器打开文件
su.setContentDisposition(null);
su.downloadFile(filePath);
} catch(Exception e) {
e.printStackTrace();
flag = false;
}
return flag;
}
最后附上下载文件效果图:
相关文章推荐
- Java Web中使用JSPSmartUpload控件实现文件的上传和下载(解决了中文乱码问题)(JSP页面采用GBK编码)
- 真正解决jspSmartUpload组件上传下载文件时中文乱码问题!!!
- Java Web中使用JSPSmartUpload控件实现文件的上传和下载(解决了中文乱码问题)(JSP页面采用GBK编码)
- 关于jspsmartupload中文名文件上传后为乱码的问题
- 搞定使用jspsmartupload 组件进行上传和下载出现的乱码问题。
- 解决jspsmartupload上传下载的中文乱码问题
- jspSmartUpload实现上传下载时的中文乱码问题
- jspSmartUpload上传下载全攻略 (四、文件下载篇 )
- 项目开发技巧(四):使用JspSmartupload实现文件上传下载(二):jspSmartUpload上传下载全攻略(ZZ)
- JSP SmartUpload上传文件乱码解决纪实+UploadBean上传解决方案
- JSP SmartUpload上传文件乱码解决纪实+UploadBean上传解决方案
- jspsmartupload的上传下载时中文乱码的解决方案
- 解决jspsmartupload上传文件时大小受限制问题
- jspSmartUpload上传中文乱码问题
- 项目开发技巧(四):使用JspSmartupload实现文件上传下载(三):jspsmartupload samples
- jspsmartupload上传文件,中文乱码解决方法
- jspsmart实现文件上传下载及jspSmartUpload.jar下载
- 利用jspsmartupload组件来上传和下载文件
- jspsmart实现文件上传下载及jspSmartUpload.jar下载
- jspSmartUpload上传下载全攻略(三、文件上传篇 )