这是我的下载文件操作类,它将在浏览器中显示内容。该文件在某个路径中指定。但我需要的文件名和内容应该来自数据库,它必须显示为你想要保存/打开对话框。
package demo;
import com.opensymphony.xwork2.Action;
import org.apache.struts2.dispatcher.StreamResult;
import com.opensymphony.xwork2.ActionInvocation;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.interceptor.ServletRequestAware;
/**
*
* @author janardhan.y
*/
public class DynamicStreamResult extends StreamResult implements ServletRequestAware {
private String name;
//holds name of downloaded file
private InputStream inputStream;
//holds stream of downloaded file
private String description;
//holds the content type of the downloaded file
private long size;
//holds the content size of the downloaded file
private HttpServletRequest servletRequest;
@Override
public void doExecute(String finalLocation, ActionInvocation invocation) throws Exception {
System.out.println("DDDDDDDDDDDDDDDDDD ");
//get name of downloaded file
String downloadedFileName = (String) invocation.getStack().findValue(conditionalParse("name", invocation));
System.out.println("SSSSSSSSS " +downloadedFileName);
contentDisposition = "filename=\""+ downloadedFileName + "\"";
System.out.println("FFFFFFFFFFFFF " +contentDisposition);
//get file size
contentLength = "" + invocation.getStack().findValue(conditionalParse("size", invocation));
// get type of file
contentType = "" + invocation.getStack().findValue(conditionalParse("description", invocation));
/*
Executes the result given a final location
(jsp page, action, etc) and
the action invocation (the state in which
the action was executed).
*/
super.doExecute(finalLocation, invocation);
}
public String downloadFile() throws FileNotFoundException {
/*
let, method searchFile(String fileName)
does the searching for us
& returns InputStream of the file if found
and null otherwise.
*/
this.inputStream = searchFile(name);
System.out.println("IIIII " +inputName.toString());
if (inputStream != null) {
System.out.println("IIIIIIIIIIIN");
return Action.SUCCESS;
} else {
//handle error
System.out.println("EEEEEEEEEEEEEE");
return Action.ERROR;
}
}
public InputStream getInputStream() throws Exception {
return inputStream;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return this.description;
}
public void setDescription(String description) {
this.description = description;
}
public long getSize() {
return size;
}
public void setSize(long size) {
this.size = size;
}
public InputStream searchFile(String name) throws FileNotFoundException {
String filePath = servletRequest.getSession().getServletContext().getRealPath("/");
System.out.println("FFFFFFFFFFFFF " + filePath);
System.out.println("NNNNNNNNNNNNNNN " + name);
// File file = new File(filePath,name);
File file = new File("D:\\Desktop13\\All Document\\MY Projects\\New Folder (2)\\STRUTS2CRUD\\build\\web\\j.txt");
System.out.println("JJJJJJ " + file.toString());
inputStream = new FileInputStream(file.toString());
return inputStream;
}
public HttpServletRequest getServletRequest() {
return servletRequest;
}
@Override
public void setServletRequest(HttpServletRequest servletRequest) {
this.servletRequest = servletRequest;
}
}
这是我的struts.xml代码
<struts>
<package name="default" extends="hibernate-default">
<result-types>
<result-type name="myStream" default="false" class="demo.DynamicStreamResult" />
</result-types>
<!-- action for downloading file-->
<action name="downloadFile" method="downloadFile" class="demo.DynamicStreamResult">
<result type="myStream"/>
<result name="error">jsps/your_error_page.jsp</result>
<result name="success" type="stream">
<param name="contentType">image/jpeg, text/plain</param>
<param name="inputName">imageStream</param>
<param name="contentDisposition">filename="j.txt"</param>
<param name="bufferSize">1024</param>
</result>
</action>
</package>
</struts>
答案 0 :(得分:0)
检查以下代码。
这将采取文件&amp;数据库中的文件名&amp;将显示保存/打开对话框。
public class DownloadResume extends ActionSupport implements SessionAware {
private int stud_id;
private Map session;
private String fileName;
private String contentType;
private String emessage;
// getters & setters
@Override
public String execute() throws Exception {
getResume();
return "none";
}
//Download Resume from database
private void getResume() throws Exception {
int id = getStud_id();
Blob b;
String fileName = "";
Connection con = DatabaseConnection.GetConnection();
PreparedStatement ps = con.prepareStatement("select stud_resume, stud_resume_content_type, stud_resume_file_name from table_stud_resume where stud_id=?");
ps.setInt(1, id);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
fileName = rs.getString("stud_resume_file_name");
b = rs.getBlob("stud_resume");
//Download resume directly on client side without downloading on server
HttpServletResponse response = ServletActionContext.getResponse();
response.reset();
response.addHeader("Content-Disposition", "attachment; filename=\""+fileName+"\"");
response.getOutputStream().write(b.getBytes(1, (int)b.length()));
response.getOutputStream().flush();
response.getOutputStream().close();
}
}
struts.xml
配置
<action name="DownloadResume" class="myPackage.DownloadResume">
</action>
这里我们直接将内容写入outputstream,因此无需返回任何结果或结果类型(即流)。
希望这能解决您的问题。
答案 1 :(得分:0)
我认为您的以下行正在产生问题。
inputStream = new FileInputStream(file.toString());
为什么在这里使用file.toString()
。
只需将您的文件传递给以下&amp;检查。
inputStream = new FileInputStream(file);