Home > Back-end >  For help, always set a Java web download file header. The Response already committed.
For help, always set a Java web download file header. The Response already committed.

Time:10-30

The code below
 
Private String dowmLoadFile (String filename, HttpServletResponse response) {
Response. AddHeader (" the content-type ", "application/octet - stream");
OutputStream OS=null;
Enclosing the logger. The error ("====================need to download the file name: "+ filename);
String path=ftpConfig. OptString (" SFTP. Path ");
String curPath=(String) UniFtpUtil. CurFilePath. The get ();
if (! StringUtil. IsEmpty (curPath)) {
Path=curPath + "/"
UniFtpUtil. CurFilePath. Set (" ");
}
ChannelSftp channel=null;
Try {
OS=response. GetOutputStream ();
The channel=getChannel ();
The Vector ls=channel. The ls (path);
Enclosing the logger. The error ("====================server of the communist party of China "+ ls. The size () +" file ");
If ((ls!=null) & amp; & (ls. The size () & gt; {0))
String filenamei="";
for (int i=0; i ChannelSftp. LsEntry entry=(ChannelSftp. LsEntry) ls. Get (I);
Filenamei=entry. GetFilename ();
Enclosing the logger. The error ("====================server first "+ I +" file file name: "+ filenamei);
{if (filenamei. The contains (filename))
Filename=filenamei;
String headName=filename. The substring (0, filename. The lastIndexOf (". "));
String tailName=filename. The substring (filename. LastIndexOf (". "));
response.addHeader("Content-Disposition", "attachment; Filename="+ new String (headName getBytes (" GBK"), "ISO - 8859-1) + tailName);
Enclosing the logger. The error ("====================begin to download file: "+ + filename path);
Channel. The get (path + filename, OS, null);
Close (channel);
Return the filename.
}
}
Enclosing the logger. The error ("====================no matching files in the server ");
} else {
Enclosing the logger. The error ("====================file does not exist the====================");
Throw new RuntimeException (download "failure");
}
{} the catch (Exception ex)
Ex. PrintStackTrace ();
This. The logger. The error (" download downFile method error ");
Throw new RuntimeException (download "failure");
} the finally {
Try {
os.close();
Close (channel);
} the catch (IOException e) {
e.printStackTrace();
}
}
return null;
}

An error log

CodePudding user response:

There are big?

CodePudding user response:

From the point of this warning, meaning is clear, response, and submit to the client, so can't set the Header parameter,
But from the point of your code and log, is a bit odd,
According to your log to see, first print your post code line 4,
Then the next log is code 41 directly, 41 in the catch, and also is an exception occurs, that is to say must say one line of code that executes on the affirmation in the try, and there is no print any logs, then from the point of the code, line 15 to perform the most, because the line 16 print log, but not the console, therefore, should be the following three lines of an error:
OS=response. GetOutputStream ();
The channel=getChannel ();
The Vector ls=channel. The ls (path);
The key is the OS, and then finally closed, also is the end of the response, the whole process is not set any header ah, is behind the rest of set,

Or say this log has a problem?

CodePudding user response:

refer to the second floor small fat is my response:
from the point of this warning, meaning is clear, response, and submit to the client, so can't set the Header parameter,
But from the point of your code and log, is a bit odd,
According to your log to see, first print your post code line 4,
Then the next log is code 41 directly, 41 in the catch, and also is an exception occurs, that is to say must say one line of code that executes on the affirmation in the try, and there is no print any logs, then from the point of the code, line 15 to perform the most, because the line 16 print log, but not the console, therefore, should be the following three lines of an error:
OS=response. GetOutputStream ();
The channel=getChannel ();
The Vector ls=channel. The ls (path);
The key is the OS, and then finally closed, also is the end of the response, the whole process is not set any header ah, is behind the rest of set,

Or say this log has a problem?

I also didn't find a place to set up any header, in the system and a method is also called the eventually, no error, but there is a error, don't know how to troubleshoot

CodePudding user response:

reference learning know how reply: 3/f
Quote: refer to the second floor small fat is my response:

From the point of this warning, meaning is clear, response, and submit to the client, so can't set the Header parameter,
But from the point of your code and log, is a bit odd,
According to your log to see, first print your post code line 4,
Then the next log is code 41 directly, 41 in the catch, and also is an exception occurs, that is to say must say one line of code that executes on the affirmation in the try, and there is no print any logs, then from the point of the code, line 15 to perform the most, because the line 16 print log, but not the console, therefore, should be the following three lines of an error:
OS=response. GetOutputStream ();
The channel=getChannel ();
The Vector ls=channel. The ls (path);
The key is the OS, and then finally closed, also is the end of the response, the whole process is not set any header ah, is behind the rest of set,

Or say this log has a problem?

I also didn't find a place to set up any header, in the system and a method is also called the eventually, no error, but there is a error, don't know how to troubleshoot


But indeed you log the error, the file download failed, how are you getting along with the code, the first look, and see if I can get any useful information,
  • Related