|
|
@ -0,0 +1,120 @@
|
|
|
|
|
|
|
|
package com.demo.files;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import java.io.DataOutputStream;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
|
|
|
|
import java.io.FileInputStream;
|
|
|
|
|
|
|
|
import java.io.FileOutputStream;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
|
|
|
import java.io.InputStream;
|
|
|
|
|
|
|
|
import java.io.OutputStream;
|
|
|
|
|
|
|
|
import java.io.PrintWriter;
|
|
|
|
|
|
|
|
import java.net.URLEncoder;
|
|
|
|
|
|
|
|
import java.sql.Blob;
|
|
|
|
|
|
|
|
import java.sql.Connection;
|
|
|
|
|
|
|
|
import java.sql.PreparedStatement;
|
|
|
|
|
|
|
|
import java.sql.ResultSet;
|
|
|
|
|
|
|
|
import java.sql.SQLException;
|
|
|
|
|
|
|
|
import java.sql.Statement;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import javax.servlet.RequestDispatcher;
|
|
|
|
|
|
|
|
import javax.servlet.ServletException;
|
|
|
|
|
|
|
|
import javax.servlet.ServletOutputStream;
|
|
|
|
|
|
|
|
import javax.servlet.annotation.WebServlet;
|
|
|
|
|
|
|
|
import javax.servlet.http.HttpServlet;
|
|
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
|
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
|
|
|
|
|
import javax.sql.rowset.serial.SerialBlob;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import org.apache.commons.fileupload.FileItem;
|
|
|
|
|
|
|
|
import org.apache.commons.fileupload.FileUploadException;
|
|
|
|
|
|
|
|
import org.apache.commons.io.IOUtils;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import com.domain.GetDBConnection;
|
|
|
|
|
|
|
|
import com.domain.Types;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import net.sf.json.JSONObject;
|
|
|
|
|
|
|
|
import net.sourceforge.tess4j.Tesseract;
|
|
|
|
|
|
|
|
import net.sourceforge.tess4j.TesseractException;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Servlet implementation class ReadFileServlet
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
@WebServlet("/readfil-servlet")
|
|
|
|
|
|
|
|
public class ReadFileServlet extends HttpServlet {
|
|
|
|
|
|
|
|
private static final long serialVersionUID = 1L;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* @see HttpServlet#HttpServlet()
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
public ReadFileServlet() {
|
|
|
|
|
|
|
|
super();
|
|
|
|
|
|
|
|
// TODO Auto-generated constructor stub
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
|
|
|
|
|
|
|
// TODO Auto-generated method stub
|
|
|
|
|
|
|
|
Connection con=null;
|
|
|
|
|
|
|
|
Statement sql;
|
|
|
|
|
|
|
|
ResultSet rs;
|
|
|
|
|
|
|
|
PreparedStatement preSql;
|
|
|
|
|
|
|
|
con = GetDBConnection.connectDB("musicMySQL","root","root");
|
|
|
|
|
|
|
|
if(con == null ) return;
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
sql=con.createStatement();
|
|
|
|
|
|
|
|
con.setAutoCommit(false);//程序必须调用commit或者rollback方法
|
|
|
|
|
|
|
|
String sqlStr ="SELECT * FROM music WHERE filename=?";
|
|
|
|
|
|
|
|
preSql = con.prepareStatement(sqlStr);
|
|
|
|
|
|
|
|
String filename = request.getParameter("filename");
|
|
|
|
|
|
|
|
filename = new String(filename.getBytes("iso8859-1"),"UTF-8");
|
|
|
|
|
|
|
|
System.out.println(filename);
|
|
|
|
|
|
|
|
preSql.setString(1, filename);
|
|
|
|
|
|
|
|
rs=preSql.executeQuery();
|
|
|
|
|
|
|
|
while(rs.next()) {
|
|
|
|
|
|
|
|
// String filename=rs.getString("filename");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
String filepath = request.getSession().getServletContext().getRealPath("/WEB-INF/upload/"+filename);
|
|
|
|
|
|
|
|
Blob blob=rs.getBlob("musicMIDI");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
InputStream in=blob.getBinaryStream();
|
|
|
|
|
|
|
|
File file = new File(filepath);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
OutputStream out=new FileOutputStream(file);
|
|
|
|
|
|
|
|
FileInputStream is = new FileInputStream(file);
|
|
|
|
|
|
|
|
IOUtils.copy(in, out);
|
|
|
|
|
|
|
|
//设置相应类型让浏览器知道用什么打开 用application/octet-stream也可以,看是什么浏览器
|
|
|
|
|
|
|
|
response.setContentType("application/x-msdownload");
|
|
|
|
|
|
|
|
//设置头信息
|
|
|
|
|
|
|
|
response.setHeader("Content-Disposition", "attachment;filename=\"" + filename + "\"");
|
|
|
|
|
|
|
|
//InputStream inputStream = new FileInputStream(file);
|
|
|
|
|
|
|
|
OutputStream ouputStream = response.getOutputStream();
|
|
|
|
|
|
|
|
//ouputStream.write(filename.getBytes());
|
|
|
|
|
|
|
|
byte b[] = new byte[1024];
|
|
|
|
|
|
|
|
int n =0;
|
|
|
|
|
|
|
|
while((n = is.read(b)) != -1){
|
|
|
|
|
|
|
|
ouputStream.write(b,0,n);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
IOUtils.copy(in, ouputStream);
|
|
|
|
|
|
|
|
//关闭流
|
|
|
|
|
|
|
|
ouputStream.close();
|
|
|
|
|
|
|
|
in.close();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
//RequestDispatcher view = request.getRequestDispatcher("/index.jsp");
|
|
|
|
|
|
|
|
//view.forward(request, response);
|
|
|
|
|
|
|
|
}catch(SQLException e) {
|
|
|
|
|
|
|
|
//RequestDispatcher view = request.getRequestDispatcher("/index.jsp");
|
|
|
|
|
|
|
|
//view.forward(request, response);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
|
|
|
// TODO Auto-generated catch block
|
|
|
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|