struts2文件上传下载及乱码问题解决
2008-11-11 09:03
477 查看
java和jsp文件的编码都是UTF-8 的
public void setInputPath(String value) throws UnsupportedEncodingException {
inputPath = new String(value.getBytes("ISO-8859-1"),"UTF-8");
}
public InputStream getInputStream() throws Exception {
if(log.isDebugEnabled()){
log.debug("inputPath="+inputPath);
}
return new java.io.FileInputStream(inputPath);
// return ServletActionContext.getServletContext().getResourceAsStream(inputPath);
//使用上面的语句将无法获取到inputStream,如果资源恰好在web应用下面,则是可以的。
}
/**
* 下载日志文件
* @return
* @throws Excepiton
*/
public String download () throws Exception{
String fileName = inputPath.substring(inputPath.lastIndexOf(java.io.File.separator)+1, inputPath.length());
if(log.isDebugEnabled()){
log.debug("fileName="+fileName);
}
ServletActionContext.getResponse().setHeader("Content-Disposition", "attachment; filename="+new String(fileName.getBytes("gb2312"),"iso-8859-1"));
return SUCCESS;
}
注意:第一个转换,"ISO-8859-1"————"UTF-8" UTF-8是根据你自己的编码来处理
第二个转换,"gb2312"————"iso-8859-1" 你就不要改变了,不管你是什么编码,都这么处理就是了,只要你的客户用的是中文的操作系统,呵呵
配置文件的内容
<action name="download" class="com.work.qxgl.loglogin.LogLoginAction"
method="downloadLogs">
<result name="success" type="stream">
<param name="inputName">inputStream</param>
<param name="bufferSize">4096</param>
</result>
</action>
下载设置
<a href='<s:url action="download" namespace="/qxglloglogin"><s:param name="inputPath" value="filePath" /></s:url>'><s:property value="fileName" /></a>
return upload;
}
public void setUpload(File upload) {
this.upload = upload;
}
// since we are using <s:file name="upload" .../> the file name will be
// obtained through getter/setter of <file-tag-name>FileName
/**
* 如果表单域为fj,那么就叫getFjFileName;
* @return
*/
public String getUploadFileName() {
return fileName;
}
public void setUploadFileName(String fileName) {
this.fileName = fileName;
}
// since we are using <s:file name="upload" ... /> the content type will be
// obtained through getter/setter of <file-tag-name>ContentType
/**
* 如果表单域为fj,那么就叫getFjContentType
* @return
*/
public String getUploadContentType() {
return contentType;
}
public void setUploadContentType(String contentType) {
this.contentType = contentType;
}
在配置文件中限制上传文件的格式
< action name ="fileUpload" class ="tutorial.FileUploadAction" >
< interceptor-ref name ="fileUpload" >
< param name ="allowedTypes" >
image/bmp,image/png,image/gif,image/jpeg
</ param >
</ interceptor-ref >
< interceptor-ref name ="defaultStack" />
< result name ="input" > /FileUpload.jsp </ result >
< result name ="success" > /ShowUpload.jsp </ result >
</ action >
文件下载
private String inputPath;<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />public void setInputPath(String value) throws UnsupportedEncodingException {
inputPath = new String(value.getBytes("ISO-8859-1"),"UTF-8");
}
public InputStream getInputStream() throws Exception {
if(log.isDebugEnabled()){
log.debug("inputPath="+inputPath);
}
return new java.io.FileInputStream(inputPath);
// return ServletActionContext.getServletContext().getResourceAsStream(inputPath);
//使用上面的语句将无法获取到inputStream,如果资源恰好在web应用下面,则是可以的。
}
/**
* 下载日志文件
* @return
* @throws Excepiton
*/
public String download () throws Exception{
String fileName = inputPath.substring(inputPath.lastIndexOf(java.io.File.separator)+1, inputPath.length());
if(log.isDebugEnabled()){
log.debug("fileName="+fileName);
}
ServletActionContext.getResponse().setHeader("Content-Disposition", "attachment; filename="+new String(fileName.getBytes("gb2312"),"iso-8859-1"));
return SUCCESS;
}
注意:第一个转换,"ISO-8859-1"————"UTF-8" UTF-8是根据你自己的编码来处理
第二个转换,"gb2312"————"iso-8859-1" 你就不要改变了,不管你是什么编码,都这么处理就是了,只要你的客户用的是中文的操作系统,呵呵
配置文件的内容
<action name="download" class="com.work.qxgl.loglogin.LogLoginAction"
method="downloadLogs">
<result name="success" type="stream">
<param name="inputName">inputStream</param>
<param name="bufferSize">4096</param>
</result>
</action>
下载设置
<a href='<s:url action="download" namespace="/qxglloglogin"><s:param name="inputPath" value="filePath" /></s:url>'><s:property value="fileName" /></a>
文件上传
public File getUpload() {return upload;
}
public void setUpload(File upload) {
this.upload = upload;
}
// since we are using <s:file name="upload" .../> the file name will be
// obtained through getter/setter of <file-tag-name>FileName
/**
* 如果表单域为fj,那么就叫getFjFileName;
* @return
*/
public String getUploadFileName() {
return fileName;
}
public void setUploadFileName(String fileName) {
this.fileName = fileName;
}
// since we are using <s:file name="upload" ... /> the content type will be
// obtained through getter/setter of <file-tag-name>ContentType
/**
* 如果表单域为fj,那么就叫getFjContentType
* @return
*/
public String getUploadContentType() {
return contentType;
}
public void setUploadContentType(String contentType) {
this.contentType = contentType;
}
在配置文件中限制上传文件的格式
< action name ="fileUpload" class ="tutorial.FileUploadAction" >
< interceptor-ref name ="fileUpload" >
< param name ="allowedTypes" >
image/bmp,image/png,image/gif,image/jpeg
</ param >
</ interceptor-ref >
< interceptor-ref name ="defaultStack" />
< result name ="input" > /FileUpload.jsp </ result >
< result name ="success" > /ShowUpload.jsp </ result >
</ action >
相关文章推荐
- struts2解决文件上传下载中文乱码问题
- Struts2-文件上传下载-解决下载中文乱码问题
- Java Web中使用JSPSmartUpload控件实现文件的上传和下载(解决了中文乱码问题)(JSP页面采用GBK编码)
- 使用FPT上传下载文件和解决中文名文件乱码问题
- 真正解决jspSmartUpload组件上传下载文件时中文乱码问题!!!
- 解决Struts2文件下载中文名称乱码以及文件名称中包含空格问题
- javaWeb中文件的上传与下载及乱码问题解决
- struts2解决下载中文文件乱码问题
- javaWeb中文件的上传与下载及乱码问题解决
- 用Struts2更好的实现文件的上传、下载功能以及解决中文名称问题
- Struts2 .apk 文件下载及解决中文文件名乱码问题(转)
- struts2上传下载文件中遇到的乱码问题
- FTPClient上传和下载文件,中文乱码问题解决
- 文件上传下载解决乱码问题
- Struts2 .apk 文件下载及解决中文文件名乱码问题(转)
- struts2文件下载中文乱码问题彻底解决
- jm解决乱码问题-参数化-数据库操作-文件上传下载
- commons-FileUpload文件上传下载,及中文乱码、IE7路径错误问题解决
- Struts2文件上传与下载中的中文乱码问题解决方案
- Java Web中使用JSPSmartUpload控件实现文件的上传和下载(解决了中文乱码问题)(JSP页面采用GBK编码)