resteasy download文件 java端代码

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import org.springframework.stereotype.Controller;

@Controller
@Path("/public/siteDownLoad")
public class SiteUpLoad {
@POST
@Path("/download")
@Produces(MediaType.APPLICATION_OCTET_STREAM)
public void serverLogDownload(@Context HttpServletResponse response) throws Exception{
File pathsavefile = new File("c:\\test.zip");
String fileName="test.zip";//保存窗口中显示的文件名

response.reset();
response.setContentType("APPLICATION/OCTET-STREAM");
fileName=response.encodeURL(new String(fileName.getBytes(),"UTF-8"));//转码
response.setHeader("Content-Disposition", "attachment; filename=\""+fileName+"\"");
ServletOutputStream out = response.getOutputStream();
InputStream inStream=new FileInputStream(pathsavefile);
byte[] b = new byte[1024];
int len;
while((len=inStream.read(b)) >0)
out.write(b,0,len);
response.setStatus( response.SC_OK );
response.flushBuffer();
out.close();
inStream.close();
}
}

你可能感兴趣的:(servlet,下载文件,resteasy)