| |

VerySource

 Forgot password?
 Register
Search
View: 1382|Reply: 13

Use struts to achieve download, the Chinese file name is garbled when downloading, I have tried encoding conversion, no

[Copy link]

1

Threads

9

Posts

8.00

Credits

Newbie

Rank: 1

Credits
8.00

 China

Post time: 2020-2-6 11:30:01
| Show all posts |Read mode
Rewrite process in ActionServlet, add a line
request.setCharacterEncoding ("GB2312");

The download method in the action class is as follows:
private boolean download (String id, HttpServletRequest request, HttpServletResponse response) {
// Set the response header and file name for download

Ranch
response.reset ();
response.setContentType ("APPLICATION / OCTET-STREAM; charset = GB2312");
// try {
response.setHeader ("Content-Disposition",
"attachment; filename =\" "+ id +" .jar\"");
//} catch (UnsupportedEncodingException e) {
// System.out.println (e);
//}
if (debug) {
System.out.println ("download at");
}
boolean b = false;
DbConnection db = null;
Connection conn = null;
Statement st = null;
String sql = "select CONTENT from DIC_PLUGINS where id = '" + id + "'";
if (debug) {
System.out.println (sql);
}
ResultSet rs = null;
Blob blob = null;
try {
db = new DbConnection ();
db.openConnection ();
conn = db.getConnection ();
st = conn.createStatement ();
rs = st.executeQuery (sql);
if (rs.next ()) {
blob = rs.getBlob ("CONTENT");
}
} catch (Exception e) {
System.out.println (e);
} finally {
try {
if (rs! = null)
rs.close ();
if (st! = null)
st.close ();
if (conn! = null)
conn.close ();
if (db! = null)
db.close ();
} catch (Exception e) {
System.out.println (e);
}
}
if (debug) {
System.out.println ("write");
}
// write stream information
if (blob! = null) {
InputStream fileInputStream = null;
int size = 0;
try {
fileInputStream = blob.getBinaryStream ();
size = (int) blob.length ();
} catch (SQLException e) {
System.out.println (e);
}
byte [] bytes = new byte [size];
// fileInputStream.read (bytes);
int i = 0;
Ranch
if (debug) {
System.out.println ("last");
}
try {
while ((i = fileInputStream.read (bytes))! = -1) {
response.getOutputStream (). write (bytes, 0, i);
}
Ranch
fileInputStream.close ();
response.getOutputStream (). flush ();
b = true;
} catch (Exception e) {
System.out.println (e);
}
}
return b;
}
Reply

Use magic Report

0

Threads

63

Posts

42.00

Credits

Newbie

Rank: 1

Credits
42.00

 China

Post time: 2020-3-26 13:45:02
| Show all posts
response.setContentType ("APPLICATION / OCTET-STREAM");
// try {
response.setHeader ("Content-Disposition",
"attachment; filename =\" "+ new String (id, request.getCharacterEncoding ()) +" .jar\"");

If you remember correctly
Reply

Use magic Report

1

Threads

9

Posts

8.00

Credits

Newbie

Rank: 1

Credits
8.00

 China

 Author| Post time: 2020-3-28 19:30:01
| Show all posts
The problem still exists, the code I modified is
response.setContentType ("APPLICATION / OCTET-STREAM");
Ranch
try {
response.setHeader ("Content-Disposition", "attachment; filename =\" "+ new String (id.getBytes (), request.getCharacterEncoding ()) +" .jar\"");
// response.setHeader ("Content-Disposition",
// "attachment; filename =\" "+ id +" .jar\"");
} catch (UnsupportedEncodingException e) {
System.out.println (e);
}
Reply

Use magic Report

1

Threads

9

Posts

8.00

Credits

Newbie

Rank: 1

Credits
8.00

 China

 Author| Post time: 2020-3-28 23:45:01
| Show all posts
And the request encoding output is GB2312, but the download is garbled
Reply

Use magic Report

0

Threads

63

Posts

42.00

Credits

Newbie

Rank: 1

Credits
42.00

 China

Post time: 2020-3-29 18:45:02
| Show all posts
sorry, reversed

String outFile = new String ("Ahhhhhhh.jar" .getBytes (request.getCharacterEncoding ()), "iso-8859-1");
Reply

Use magic Report

1

Threads

9

Posts

8.00

Credits

Newbie

Rank: 1

Credits
8.00

 China

 Author| Post time: 2020-4-2 14:00:01
| Show all posts
The garbled has changed, but the problem remains, the code:
response.setContentType ("APPLICATION / OCTET-STREAM");
Ranch
try {
String filename = new String ((id + ". Jar"). GetBytes (request.getCharacterEncoding ()), "iso-8859-1");
response.setHeader ("Content-Disposition", "attachment; filename =\" "+ filename +"\"");
Reply

Use magic Report

0

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-4-7 10:30:01
| Show all posts
What about other codes?
Reply

Use magic Report

1

Threads

9

Posts

8.00

Credits

Newbie

Rank: 1

Credits
8.00

 China

 Author| Post time: 2020-4-7 17:15:01
| Show all posts
I tried everything with GBK. I used iso-8859-1 when I did n’t use struts. I rewrote it with struts, but I found that the Chinese problem could not be solved.
Reply

Use magic Report

0

Threads

63

Posts

42.00

Credits

Newbie

Rank: 1

Credits
42.00

 China

Post time: 2020-4-9 14:15:01
| Show all posts
I ’ll try again. I just wrote a JSP, and one of the codes was successful, but because of the IE cache, I ca n’t remember the code for a while.
Reply

Use magic Report

0

Threads

63

Posts

42.00

Credits

Newbie

Rank: 1

Credits
42.00

 China

Post time: 2020-4-10 10:00:01
| Show all posts
<% @ page pageEncoding = "UTF-8" contentType = "application / octet-stream"%>
<% String filename = java.net.URLEncoder.encode ("Hello", "UTF-8") + ".txt";%>
<% response.setHeader ("Content-Disposition", "attachment; filename =\" "+ filename +"\"");%>
<% out.write (65); out.write (66);%>
<% out.flush ();%>
<% System.out.println (new java.util.Date () + "/" + filename);%>

UTF-8 is used for my test, depending on your needs
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list