2013年9月23日星期一

springMVC how to download files ?

My database, a table holds three parameters, namely : id, name ( file name ), path ( file path on the server ) ;
this case, the corresponding file on the server .
I want to download it to your local computer , not struts2, download how to write code , controller should be how to write ? How can we achieve and struts2 download the same effect.
just contact springMVC, had previously been used struts2 , now with springMVC would not, her brother to give pointers , it is best to attach the code , thank you, all clubs offer
---- - Solution --------------------------------------------
this SpringMVC nothing , and I have done in the jsp download a picture , you might want to modify the code under

response.setContentType ("application / x-download");
/ / application.getRealPath ("/ main / mvplayer / CapSetup.msi"); Gets the physical path
String filedownload = request.getRealPath ("/") + "/ organization / img /" + " organization chart . png";
String filedisplay = " organization chart . png";
filedisplay = URLEncoder.encode (filedisplay, "UTF-8");
response.addHeader ("Content-Disposition", "attachment; filename =" + filedisplay);

java.io.OutputStream outp = null;
java.io.FileInputStream in = null;
try
{
outp = response.getOutputStream ();
in = new java.io.FileInputStream (filedownload);

byte [] b = new byte [1024];
int i = 0;

while ((i = in.read (b))> 0)
{
outp.write (b, 0, i);
}
outp.flush ();
/ / to add the following two sentences, otherwise it will error
/ / java.lang.IllegalStateException: getOutputStream () has already been called for this respons
out.clear ();
out = pageContext.pushBody ();
}
catch (Exception e)
{
System.out.println ("Error!");
e.printStackTrace ();
}
finally
{
if (in! = null)
{
in.close ();
in = null;
}

if (in! = null)
{
in.close ();
in = null;
}
}


they find online is also very good looking
------ Solution --------------------------- -----------------
/ / download methods
private void downloadFile (File file) {
String fileName = file.getName ();
try {
fileName = URLEncoder.encode (fileName, "UTF-8");
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace ();
}
contextPvd.getResponse (). setContentType ("application / zip");
contextPvd.getResponse (). addHeader ("Content-Disposition", "attachment; filename =" + fileName);
OutputStream outp = null;
FileInputStream in = null;
try {
outp = contextPvd.getResponse (). getOutputStream ();
in = new FileInputStream (file);
byte [] b = new byte [1024];
int i = 0;

while ((i = in.read (b))> 0) {
outp.write (b, 0, i);
}
outp.flush ();
} catch (Exception e) {
/ / log.error ("", e);
} finally {
if (in! = null) {
try {
in.close ();
in = null;
} catch (IOException e) {
e.printStackTrace ();
}
}
if (outp! = null) {
try {
outp.close ();
outp = null;
} catch (IOException e) {
e.printStackTrace ();
}
}
}
}
------ For reference only --------------------------------- ------
pro, I can add a few points , and do not let the message sink anymore, okay ? Come on ......
------ For reference only ------------------------------- --------
download the file, it does not matter with springmvc struts2 ,
Since there is a path , the path of the corresponding file also , why do not you direct href = file path , and then directly prompted to download the
------ For reference only - --------------------------------------
written directly respone body which does not respect or leave it , to be honest
file upload and download with what framework hair had nothing to do , is to rely on requestbody and reponsebody
understand the HTTP protocol on okie
------ For reference only ----------------------------- ----------

downloaded files how would corrupt and can not open ? Pro , you know what the problem is ? Gave setContentType ("image / jpeg"); does not work, you know what ?
------ For reference only -------------------------------------- -
Thank you , possibly on very poor , had previously been using struts2 package good things that he wrote it on the problem , problem solving, Results posted to the sub .

没有评论:

发表评论