有没大神教教我java怎么实现文件下载的,不要超链接那种,要有action求jsp和action
1个回答
展开全部
<a href="control/downFile.do?pistrPath=' + rec.get('path') + '">'
请求到后台,把文件地址传到后台
/**
* 下载文件
* @param pistrPath
* @param piobjResponse
* @throws MalformedURLException
*/
public void downloadNet(String pistrPath, HttpServletResponse piobjResponse) throws MalformedURLException {
// 下载网络文件
int iByte = 0;
System.getProperties();
try {
URL objurl = new URL(pistrPath);
System.out.println(pistrPath);
URLEncoder.encode(objurl.toString(), SengisConstant.STR_CN_ENCODE);
URLConnection objConn = objurl.openConnection();
InputStream objInputStream = objConn.getInputStream();
OutputStream objOutStream = piobjResponse.getOutputStream();
byte[] arrByte = new byte[1204];
while ((iByte = objInputStream.read(arrByte)) != -1) {
objOutStream.write(arrByte, 0, iByte);
}
objOutStream.flush();
objOutStream.close();
objInputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
请求到后台,把文件地址传到后台
/**
* 下载文件
* @param pistrPath
* @param piobjResponse
* @throws MalformedURLException
*/
public void downloadNet(String pistrPath, HttpServletResponse piobjResponse) throws MalformedURLException {
// 下载网络文件
int iByte = 0;
System.getProperties();
try {
URL objurl = new URL(pistrPath);
System.out.println(pistrPath);
URLEncoder.encode(objurl.toString(), SengisConstant.STR_CN_ENCODE);
URLConnection objConn = objurl.openConnection();
InputStream objInputStream = objConn.getInputStream();
OutputStream objOutStream = piobjResponse.getOutputStream();
byte[] arrByte = new byte[1204];
while ((iByte = objInputStream.read(arrByte)) != -1) {
objOutStream.write(arrByte, 0, iByte);
}
objOutStream.flush();
objOutStream.close();
objInputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询