您的位置:首页 > Web前端 > JavaScript

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;
}
最后附上下载文件效果图:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐